Reputation: 787
I have a large array in which certain row values repeat (8 repeats max). How can I only pull the repeated row values and their subsequent columns?
I should mention that the first column is an ID column (where I want to look for repeats), but the repeated ID values have different column values.
ID = [1;2;4;10;7;1;5;4]
I want to locate row n of both instances of 1, and 4
Output should be:
ID = [1;4;1;4]
or the sorted version:
ID=[1;1;4;4]
Either version is workable.
I believe I need a loop and the use of the find function
Upvotes: 1
Views: 566
Reputation: 20914
The sorted approach is also simple:
ID = sort(ID);
index = find(ID(2:end) == ID(1:end-1));
ID = ID(index);
Upvotes: 2
Reputation: 14939
You can use histc
, in combination with ismember
, find
and unique
this way:
[n, bin] = histc(A, unique(A));
multiple = find(n > 1);
index = find(ismember(bin, multiple))
index =
1
3
6
8
Which gives:
ID(index)
1
4
1
4
Upvotes: 2