필터 지우기
필터 지우기

Combining data with matching elements in the first two columns

조회 수: 1 (최근 30일)
Muneer
Muneer 2014년 2월 18일
댓글: Azzi Abdelmalek 2014년 2월 19일
Hello, I have a data table that looks like this
920 381 784 0
920 381 0 21.4375
23 388 1703 0
23 388 0 4.109375
445 487 304 0
445 487 0 15.09375
1100 506 1480 0
1100 506 0 28.234375
245 520 454 0
245 520 0 40.21875
For all the entries where the first two columns match (ie, the first two rows), I would like to combine those two rows into one. So these two rows:
920 381 784 0
920 381 0 21.4375
Will become
920 381 784 21.4375
And so on for the rest of the data set. I would appreciate any help.
Thanks
  댓글 수: 2
the cyclist
the cyclist 2014년 2월 18일
Is that exact pattern guaranteed? Namely, will the first zero always be in column 4, and the second zero always be in column 3? And will there always be a pair of rows like that?
Muneer
Muneer 2014년 2월 18일
No it isn't guaranteed that there will always be a pair of rows. If there is one row with no matching row, however, it will have a value in column three, not column four. Thanks

댓글을 달려면 로그인하십시오.

채택된 답변

the cyclist
the cyclist 2014년 2월 18일
편집: the cyclist 2014년 2월 19일
Here's one way:
[~,i,j]=unique(M(:,1:2),'rows');
[M(i,[1 2]),accumarray(j,M(:,3)),accumarray(j,M(:,4))]
where M is your original array.
EDIT: Original solution only gave the last two columns, so I fixed it to give all the columns you need.
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 18일
This does not give the result
Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 19일
The cyclist, your code doesn't work if there are more then two duplicate rows (I mean duplicate for the two first column).

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 18일
편집: Azzi Abdelmalek 2014년 2월 18일
M=[920 381 784 0
920 381 0 21.4375
23 388 1703 0
23 388 0 4.109375
445 487 304 0
445 487 0 15.09375
1100 506 1480 0
1100 506 0 28.234375
245 520 454 0
245 520 0 40.21875]
[~,idx,jj]=unique(M(:,1:2),'rows','stable'); % M is your matrix
n=numel(idx)
out=zeros(n,size(M,2))
for k=1:n
out(k,:)=max(M(find(jj==k),:))
end
  댓글 수: 4
Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 18일
Look at edited answer
Muneer
Muneer 2014년 2월 18일
Thanks for the help!

댓글을 달려면 로그인하십시오.

카테고리

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