# finding an item in an array

1 view (last 30 days)
MURTADHA ALDEER on 20 Mar 2012
Hello,
I have an array of five items for example 1 2 2 2 4
How can I find the positions of the repeated items, i.e 2 2 2?
Thanks

Aldin on 20 Mar 2012
Here is my solution:
a = [ 1 2 2 2 4 ];
find(a==2)
ans =
2 3 4
##### 2 CommentsShowHide 1 older comment
Aldin on 20 Mar 2012
HERE IS THE COMBINATION OF Daniels AND my CODE:
x = [1 2 2 2 4 5 5 3 3 2];
a = unique(x);
[~,b] = unique(x, 'first');
[~,c] = unique(x, 'last');
ismember(x, a(b~=c))
the result will be: 0 1 1 1 0 1 1 1 1 1
(my code) if you want to get index:
find(ans==1)
result:
ans =
2 3 4 6 7 8 9 10

Jonathan Sullivan on 20 Mar 2012

MURTADHA ALDEER on 20 Mar 2012
when I use findseq, I get this message
??? Undefined function or method 'findseq' for input arguments of type 'double'.
!
Jonathan Sullivan on 20 Mar 2012
Make sure you download the function. It is not built into MATLAB.

MURTADHA ALDEER on 20 Mar 2012
Oh, thank you
But could you help me and let me know how to download a function?
Oleg Komarov on 25 Mar 2012
FEX page for findseq: http://www.mathworks.com/matlabcentral/fileexchange/28113-findseq

Daniel Shub on 20 Mar 2012
One of my uglier solutions ...
x = [1 2 2 2 4];
a = unique(x);
[~,b] = unique(x, 'first');
[~,c] = unique(x, 'last');
ismember(x, a(b~=c))

Aldin on 21 Mar 2012
HERE IS THE COMBINATION OF Daniels AND my CODE:
x = [1 2 2 2 4 5 5 3 3 2];
a = unique(x);
[~,b] = unique(x, 'first');
[~,c] = unique(x, 'last');
ismember(x, a(b~=c))
the result will be: 0 1 1 1 0 1 1 1 1 1
(my code) if you want to get index:
find(ans==1)
result: ans = 2 3 4 6 7 8 9 10

MURTADHA ALDEER on 22 Mar 2012
Thank you all for your answer, I was looking for the most repeated item
I sat with a friend and we found the solution, see below:
previous=[1 2 2 3 4]
for lo = 1: length (previous)
for lon = 1 : length (previous)
H(lo,lon) = any(previous(lo)==previous(lon));
end
end
add will return the positions of the repeated item (2).
Regards
Daniel Shub on 25 Mar 2012