Combining data with matching elements in the first two columns
Afficher commentaires plus anciens
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 commentaires
the cyclist
le 18 Fév 2014
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
le 18 Fév 2014
Réponse acceptée
Plus de réponses (1)
Azzi Abdelmalek
le 18 Fév 2014
Modifié(e) : Azzi Abdelmalek
le 18 Fév 2014
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 commentaires
Muneer
le 18 Fév 2014
Muneer
le 18 Fév 2014
Azzi Abdelmalek
le 18 Fév 2014
Look at edited answer
Muneer
le 18 Fév 2014
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!