Info
此问题已关闭。 请重新打开它进行编辑或回答。
finding an item in an array
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(7 个)
Aldin
2012-3-20
Here is my solution:
a = [ 1 2 2 2 4 ];
find(a==2)
ans =
2 3 4
2 个评论
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
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
MURTADHA ALDEER
2012-3-20
3 个评论
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
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))
0 个评论
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
0 个评论
MURTADHA ALDEER
2012-3-22
1 个评论
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!