Effacer les filtres
Effacer les filtres

How can I compute all the possible differences of the vectors in a matrix?

6 vues (au cours des 30 derniers jours)
Hi all,
I have a problem: given a matrix A such as
A = [1,2,3;
4,0,1;
2,3,5;
6,0,1];
is there any matlab function that returns a matrix with all the possible differences of rows? I mean: a matrix in which I have in the first row (1,2,3)-(4,0,1)=(-3,2,2), in the second row (1,2,3)-(2,3,5)=(-1,-1,-2) and so forth for all the rows? In other words I want to compute all the possible combinations of differences.
Thank you very much
  2 commentaires
John D'Errico
John D'Errico le 28 Nov 2018
Is row1 - row2 different from row2 - row1?
Do you want to compute the difference row1-row1?
These are important factors which you must answer for a useful solution.
Gianluca Ligresti
Gianluca Ligresti le 28 Nov 2018
Modifié(e) : Gianluca Ligresti le 28 Nov 2018
Sorry, there is NOT difference between row1-row2 and row2-row1 and I DON'T want to compute row1-row1.

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 28 Nov 2018
result = A - permute(A, [3 2 1])
would return a size(A, 1) x size(A, 2) x size(A, 1) array where result(i, :, j) is the difference between A(i, :) and A(j, :)
  5 commentaires
Guillaume
Guillaume le 29 Nov 2018
"A matrix A that is very very big (20.000 rows x 300 columns) so with this code I obtain an 'OUT OF MEMORY' error."
Regardless of the method used to generate all the combinations, you will end up with 20,000 x 19,999 combinations = 399,980,000 combinations (with 300 columns each). This will requires around 890 GB of memory to store. Even if you could store that, whatever processing you want to do with these combinations would probably take a long time.
"Is there a way to avoid this problem?"
The only way to avoid this problem is to change your algorithm so that you don't require that many combinations.
Gianluca Ligresti
Gianluca Ligresti le 29 Nov 2018
You were perfect, thanks a lot

Connectez-vous pour commenter.

Plus de réponses (2)

Honglei Chen
Honglei Chen le 28 Nov 2018
If you have Statistics Toolbox, you can try
idx = flipud(combnk(1:4,2))
A(idx(:,1),:)-A(idx(:,2),:)
HTH
  2 commentaires
Gianluca Ligresti
Gianluca Ligresti le 28 Nov 2018
This solution is good but computes only row1-row2, row1-row3, row1-row4, row2-row3, row2-row4 and row3-row4 but I'm interested in calculating also, for example, row3-row1. In other words if I have 4 rows in my matrix, for each row I want to compute the difference between that row and all the others, so three results for each packet of differences
Honglei Chen
Honglei Chen le 28 Nov 2018
Then you can do this
idx = combnk(1:4,2)
idx = sortrows([idx;fliplr(idx)])
A(idx(:,1),:)-A(idx(:,2),:)
HTH

Connectez-vous pour commenter.


Bruno Luong
Bruno Luong le 29 Nov 2018
Modifié(e) : Bruno Luong le 29 Nov 2018
>> A = [1,2,3;
4,0,1;
2,3,5;
6,0,1];
>> r2 = nchoosek(1:size(A,1),2)
r2 =
1 2
1 3
1 4
2 3
2 4
3 4
>> A(r2(:,1),:)-A(r2(:,2),:)
ans =
-3 2 2
-1 -1 -2
-5 2 2
2 -3 -4
-2 0 0
-4 3 4
If you want to difference of 2 rows AND the opposite
>> R2 = sortrows([r2;fliplr(r2)])
R2 =
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3
>> A(R2(:,1),:)-A(R2(:,2),:)
ans =
-3 2 2
-1 -1 -2
-5 2 2
3 -2 -2
2 -3 -4
-2 0 0
1 1 2
-2 3 4
-4 3 4
5 -2 -2
2 0 0
4 -3 -4

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by