Prev: Help, slopes, maxima and minima
Next: regexp for any character sequence ( numeric or non numeric)
From: Ed Chamberlayne on 29 Sep 2009 17:52 I have a matrix Z: [ 1 24 96 2 10 20 3 12 27 4 14 40 5 16 50 6 18 60 7 20 72 8 22 85 1 34 116 2 20 40 1 36 123 2 22 47 3 24 54] I would like to check for duplicate entries in the 2nd column. I want to keep the row that has the highest 3rd column entry and then delete the other rows. For instance, the value of 20 is repeated in the 2nd column twice. I want to compare row [7 20 72] and row [2 20 40]. Since 72 is greater than 40, I want to then delete row [2 20 40]. Does this make sense? I'm having trouble separating out the duplicate entries. Then it should be a max function. Then setting the row that needs to be deleted to []; Thanks, Ed
From: Jos on 30 Sep 2009 04:19 "Ed Chamberlayne" <chambere(a)gmail.com> wrote in message <h9tvi0$4$1(a)fred.mathworks.com>... > I have a matrix Z: > [ 1 24 96 > 2 10 20 > 3 12 27 > 4 14 40 > 5 16 50 > 6 18 60 > 7 20 72 > 8 22 85 > 1 34 116 > 2 20 40 > 1 36 123 > 2 22 47 > 3 24 54] > > I would like to check for duplicate entries in the 2nd column. I want to keep the row that has the highest 3rd column entry and then delete the other rows. > > For instance, the value of 20 is repeated in the 2nd column twice. I want to compare row [7 20 72] and row [2 20 40]. Since 72 is greater than 40, I want to then delete row [2 20 40]. Does this make sense? > > I'm having trouble separating out the duplicate entries. Then it should be a max function. Then setting the row that needs to be deleted to []; > > Thanks, > > Ed First sort the matrix according to the 2nd and 3rd column using sortrows, then apply UNIQUE on the second column. % data: keep row 2 and row 4 (here indicated by 1st column) M = [1 1 2 ; 2 2 6 ; 3 2 1 ; 4 1 5 ; 5 2 3] M2 = sortrows(M,[2 3]) [dummy, idx] = unique(M2(:,2)) R = M2(idx,:) hth Jos
|
Pages: 1 Prev: Help, slopes, maxima and minima Next: regexp for any character sequence ( numeric or non numeric) |