how can i find same values in an array?
Show older comments
I have an array a, where
a=[303.1093
94.5581
86.6591
108.1389
303.1093
107.4338
138.6919
106.8294
108.1389
149.3491
187.8940
125.0922
96.7580
303.1093
75.9192
96.2085
118.8788
303.1093
303.1093
303.1093]
then i wanna do
b=round(a);
In b I want to find the positions who are having same values and I want to mark those value as 1 and others 0.
please help me.
Thank you
-- Suchi
2 Comments
Meghana Dinesh
on 5 Feb 2016
Edited: Meghana Dinesh
on 5 Feb 2016
How much "similar"? Or do you mean "same"?
suchismita
on 5 Feb 2016
Edited: suchismita
on 5 Feb 2016
Accepted Answer
More Answers (3)
Stephen23
on 5 Feb 2016
This identifies all matching rounded values, unlike the accepted answer:
b = round(a(:));
c = bsxfun(@eq,b,b.');
x = any(tril(c,-1)|triu(c,1),1);
[a(:),+x(:)]
303.1093 1
94.5581 0
86.6591 0
108.1389 1
303.1093 1
107.4338 1
138.6919 0
106.8294 1
108.1389 1
149.3491 0
187.8940 0
125.0922 0
96.7580 0
303.1093 1
75.9192 0
96.2085 0
118.8788 0
303.1093 1
303.1093 1
303.1093 1
1 Comment
suchismita
on 6 Feb 2016
Bhavini Sarkar
on 17 Sep 2016
2 votes
To find the duplicate elements of an array, first you should sort array . After sorting all duplicate elements will aggregate in adjacent positions. Now using a for loop , traverse sorted array and compare adjacent elements. If adjacent elements are equal then you found one duplicate element of array . We can also find duplicate elements of an array in linear time using some extra space.
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!