0

I had a question and got an answer yesterday about removing doubling rows in a matrix, and I can't figure out why it omits certain rows in a matrix.

With a matrix:

tmp2 =

     0    1.0000
0.1000    1.0000
0.2000    1.0000
0.3000    1.0000
0.3000    2.0000
0.4000    2.0000
0.5000    2.0000
0.6000    2.0000
0.7000    2.0000
0.7000    3.0000
0.8000    3.0000
0.9000    3.0000
1.0000    3.0000
1.1000    3.0000
1.2000    3.0000

I need to remove the rows:

0.3000    2.0000    
0.7000    3.0000

I tried to do it with

[~,b] = unique(tmp2(:,1));
tmp2(b,:) 

I wrote something on my own

tmp3 = [];

for i=1:numel(tmp2(:,1))-1
    if tmp2(i,1) == tmp3    
        tmp2(i,:) = [];
    end
    tmp3 = tmp2(i,1);
end

But all of the methods seem to omit the first row to remove... Please help, as I already spent some hours trying to fix it myself (I suck at programming...) and nothings seems to work. The matrix is an example, but generally if two rows have the same value in the first column I have to remove the second one

Community
  • 1
  • 1
dsp
  • 43
  • 1
  • 1
  • 5

1 Answers1

0

You were on the right track...

tmp2 = [...
0 1
1 1
2 3
2 5
3 5
4 7
5 4
5 8
6 1
];

Now call unique like you did, but use the flag first to grab the first unique:

[~,li]=unique(tmp2(:,1),'first');
tmp_unique = tmp2(li,:);
David_G
  • 1,127
  • 1
  • 16
  • 36