필터 지우기
필터 지우기

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