removing duplicate entries— variant in matlab
-
02-07-2021 - |
Question
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
Solution
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 ofA
, and its indices (I
) - These indices can be used to extract the rows from
A
you want.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow