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

15 visualizaciones (últimos 30 días)
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 comentarios
John D'Errico
John D'Errico el 28 de Nov. de 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 el 28 de Nov. de 2018
Editada: Gianluca Ligresti el 28 de Nov. de 2018
Sorry, there is NOT difference between row1-row2 and row2-row1 and I DON'T want to compute row1-row1.

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 28 de Nov. de 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 comentarios
Guillaume
Guillaume el 29 de Nov. de 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.

Iniciar sesión para comentar.

Más respuestas (2)

Honglei Chen
Honglei Chen el 28 de Nov. de 2018
If you have Statistics Toolbox, you can try
idx = flipud(combnk(1:4,2))
A(idx(:,1),:)-A(idx(:,2),:)
HTH
  2 comentarios
Gianluca Ligresti
Gianluca Ligresti el 28 de Nov. de 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 el 28 de Nov. de 2018
Then you can do this
idx = combnk(1:4,2)
idx = sortrows([idx;fliplr(idx)])
A(idx(:,1),:)-A(idx(:,2),:)
HTH

Iniciar sesión para comentar.


Bruno Luong
Bruno Luong el 29 de Nov. de 2018
Editada: Bruno Luong el 29 de Nov. de 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

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by