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

15 次查看(过去 30 天)
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 个评论
John D'Errico
John D'Errico 2018-11-28
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 2018-11-28
编辑:Gianluca Ligresti 2018-11-28
Sorry, there is NOT difference between row1-row2 and row2-row1 and I DON'T want to compute row1-row1.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2018-11-28
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 个评论
Guillaume
Guillaume 2018-11-29
"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.

请先登录,再进行评论。

更多回答(2 个)

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

请先登录,再进行评论。


Bruno Luong
Bruno Luong 2018-11-29
编辑:Bruno Luong 2018-11-29
>> 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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by