フィルターのクリア

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

15 ビュー (過去 30 日間)
Gianluca Ligresti
Gianluca Ligresti 2018 年 11 月 28 日
コメント済み: Gianluca Ligresti 2018 年 11 月 29 日
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.
Gianluca Ligresti
Gianluca Ligresti 2018 年 11 月 29 日
You were perfect, thanks a lot

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by