finding an item in an array
Información
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Mostrar comentarios más antiguos
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
Respuestas (7)
Aldin
el 20 de Mzo. de 2012
Here is my solution:
a = [ 1 2 2 2 4 ];
find(a==2)
ans =
2 3 4
2 comentarios
Daniel Shub
el 20 de Mzo. de 2012
+1 for simplicity. While this requires knowing what number is repeated and will fail if multiple numbers are repeated, it does answer the question.
Aldin
el 20 de Mzo. de 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
el 20 de Mzo. de 2012
0 votos
MURTADHA ALDEER
el 20 de Mzo. de 2012
0 votos
1 comentario
Jonathan Sullivan
el 20 de Mzo. de 2012
Make sure you download the function. It is not built into MATLAB.
MURTADHA ALDEER
el 20 de Mzo. de 2012
0 votos
3 comentarios
Jonathan Sullivan
el 20 de Mzo. de 2012
There's a button in the top right that says "Download All." Click that one.
MURTADHA ALDEER
el 22 de Mzo. de 2012
Oleg Komarov
el 25 de Mzo. de 2012
Diret link to download: http://www.mathworks.com/matlabcentral/fileexchange/28113-findseq?controller=file_infos&download=true
FEX page for findseq: http://www.mathworks.com/matlabcentral/fileexchange/28113-findseq
Daniel Shub
el 20 de Mzo. de 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
el 21 de Mzo. de 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
el 22 de Mzo. de 2012
1 comentario
Daniel Shub
el 25 de Mzo. de 2012
If your question is now answered, then accept your answer and upvote anyone that helped.
La pregunta está cerrada.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!