3

I have A matrix part of which looks like this :

4080530 92 -- rid of this 
4085908 92 -- keep this
4086589 93 -- rid
4091453 93 -- rid
4104393 93 -- rid
4112841 93 -- rid
4122958 93 -- rid
4130815 93 -- rid
4142617 93 -- rid
4152386 93 -- keep this
4230963 94
4242541 94
4243222 95
4253979 95

In col 2 it can be seen that some entries are repeated. I want to get rid of some of these duplicate entries. In each case I only want to keep the last value. The number of times the value is repeated need not be fixed, hence the issue. Any ideas on how I can about this.

I am currently using some of the ideas here to come up with a fix this link

bhavs
  • 2,091
  • 8
  • 36
  • 66

1 Answers1

3

This will do just that:

[~, I] = unique(A(:,2)),     
B = A(I,:);

Breakdown:

  • the unique command will output the unique, sorted version of the second column of A, and its indices (I)
  • These indices can be used to extract the rows from A you want.
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • 2
    Note that unique does retain the last element of a set of duplicates, not the first, so it does exactly what was asked for here. But in the case of a sorted vector as we have here, find(diff(A(:,2))) will also suffice for this purpose. –  Sep 19 '12 at 12:03