필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

finding an item in an array

조회 수: 2 (최근 30일)
MURTADHA ALDEER
MURTADHA ALDEER 2012년 3월 20일
마감: MATLAB Answer Bot 2021년 8월 20일
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

답변 (7개)

Aldin
Aldin 2012년 3월 20일
Here is my solution:
a = [ 1 2 2 2 4 ];
find(a==2)
ans =
2 3 4
  댓글 수: 2
Daniel Shub
Daniel Shub 2012년 3월 20일
+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
Aldin 2012년 3월 20일
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
Jonathan Sullivan 2012년 3월 20일

MURTADHA ALDEER
MURTADHA ALDEER 2012년 3월 20일
when I use findseq, I get this message
??? Undefined function or method 'findseq' for input arguments of type 'double'.
!
  댓글 수: 1
Jonathan Sullivan
Jonathan Sullivan 2012년 3월 20일
Make sure you download the function. It is not built into MATLAB.

MURTADHA ALDEER
MURTADHA ALDEER 2012년 3월 20일
Oh, thank you
But could you help me and let me know how to download a function?
  댓글 수: 3
MURTADHA ALDEER
MURTADHA ALDEER 2012년 3월 22일
Sorry, Jonathan
I can not find this button
You mean it is in Matlab, or in Matlab website?
Thank you for your valuable efforts
Oleg Komarov
Oleg Komarov 2012년 3월 25일
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
Daniel Shub 2012년 3월 20일
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
Aldin 2012년 3월 21일
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
MURTADHA ALDEER 2012년 3월 22일
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 = find(sum(H')==max(sum(H')))
add will return the positions of the repeated item (2).
Regards
  댓글 수: 1
Daniel Shub
Daniel Shub 2012년 3월 25일
If your question is now answered, then accept your answer and upvote anyone that helped.

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by