how can i find same values in an array?

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 comentarios

Meghana Dinesh
Meghana Dinesh el 5 de Feb. de 2016
Editada: Meghana Dinesh el 5 de Feb. de 2016
How much "similar"? Or do you mean "same"?
suchismita
suchismita el 5 de Feb. de 2016
Editada: suchismita el 5 de Feb. de 2016
yes same values

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 5 de Feb. de 2016
Editada: Star Strider el 5 de Feb. de 2016
The unique function will obviously provide the information you want, but ‘the positions who are having similar values’ is less clear. I believe the au (unique values in ‘a’) and ‘ic’ values (the indices in ‘a’ that correspond to them) is what you want:
[au,ia,ic] = unique(round(a),'stable')
EDIT —
‘I wanna mark those same values as 1 and others 0’
I believe this does what you want:
[au,ia] = unique(round(a),'stable');
Same = ones(size(a));
Same(ia) = 0;
Result = [a Same]
Result =
303.11 0
94.558 0
86.659 0
108.14 0
303.11 1
107.43 0
138.69 0
106.83 1
108.14 1
149.35 0
187.89 0
125.09 0
96.758 0
303.11 1
75.919 0
96.209 0
118.88 0
303.11 1
303.11 1
303.11 1
-

4 comentarios

No its not working, all 303 should show as 1 and others as 0 for example,
303.11 1
94.558 0
86.659 0
108.14 0
303.11 1
107.43 0
138.69 0
106.83 0
108.14 0
149.35 0
187.89 0
125.09 0
96.758 0
303.11 1
75.919 0
96.209 0
118.88 0
303.11 1
303.11 1
303.11 1
Stephen23
Stephen23 el 5 de Feb. de 2016
Editada: Stephen23 el 5 de Feb. de 2016
It seems that this solution is not correct, as it misses 107.43 and 108.14 (rows 6 and 4). Surely these should be marked with 1's as (rounded) they match 106.83 and 108.14 respectively (rows 8 and 9, which may or may not be marked with 1's, depending on which results you read).
See my Answer for a solution that correctly identifies all matching values.
Star Strider
Star Strider el 5 de Feb. de 2016
I wasn’t certain what you wanted. I thought you only wanted to flag the repeated values, other than the first occurrence. Since this now seems to be resolved, I’ll not pursue it further.
Stephen23
Stephen23 el 5 de Feb. de 2016
Good point about the "first occurrence". This did not occur to me, and the original question is ambiguous on this point.

Iniciar sesión para comentar.

Más respuestas (3)

Stephen23
Stephen23 el 5 de Feb. de 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 comentario

suchismita
suchismita el 6 de Feb. de 2016
i want to accept your answer but i dont how to unlike the already accepted answer?

Iniciar sesión para comentar.

Bhavini Sarkar
Bhavini Sarkar el 17 de Sept. de 2016

2 votos

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.
Azzi Abdelmalek
Azzi Abdelmalek el 5 de Feb. de 2016
[ii,jj,kk]=unique(a,'stable')

1 comentario

suchismita
suchismita el 5 de Feb. de 2016
I wanna mark those same values as 1 and others 0, how can i do that

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Feb. de 2016

Respondida:

el 17 de Sept. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by