Borrar filtros
Borrar filtros

Non-Sorted nx1 unique values

1 visualización (últimos 30 días)
William Honjas
William Honjas el 19 de Dic. de 2016
Comentada: Jan el 16 de En. de 2017
Hello, i am attempting to find repeated values in an nx1 matrix, the catch is that the unique function sorts the data. The data should not be sorted. Ideally i would like the output from my mystery function to be 1 for not repeated and 0 for repeated. Any suggestions?!
input: [1;1;1;2;3;4;5;6;6;7] output:[1;0;0;1;1;1;1;1;0;1]
the idea is that i can multiply the output and the original matrix where the data resides so that duplicate records will show as 0 values, but the first stays!
Thanks again! If you would like me to post another question please let me know!
  2 comentarios
Image Analyst
Image Analyst el 19 de Dic. de 2016
Give a short example of your input and desired output. Why don't you just use unique and histogram(). Are your data integers or floating point values?
William Honjas
William Honjas el 19 de Dic. de 2016
input: [1;1;1;2;3;4;5;6;6;7] output:[1;0;0;1;1;1;1;1;0;1]
the idea is that i can multiply the output and the original matrix where the data resides so that duplicate records will show as 0 values, but the first stays! My data are floating points

Iniciar sesión para comentar.

Respuesta aceptada

Roger Stafford
Roger Stafford el 19 de Dic. de 2016
Let x be the given n by 1 vector.
[y,p] = sort(x);
d = diff(y)~=0;
b = [d;true] & [true;d];
b(p) = b;
Then b will be your "ideal" desired result.
  8 comentarios
Roger Stafford
Roger Stafford el 21 de Dic. de 2016
@Jan. In William’s original request he states:, “the idea is that i can multiply the output and the original matrix where the data resides so that duplicate records will show as 0 values, but the first stays!” To be able to do such a multiplication (presumably elementwise) the final reordering by “b(p) = b” would be very necessary.
Jan
Jan el 16 de En. de 2017
@Roger: I meant "The data should not be sorted." Anyway, both methods are included in your solution.

Iniciar sesión para comentar.

Más respuestas (1)

John BG
John BG el 19 de Dic. de 2016
Editada: John BG el 19 de Dic. de 2016
but this code seems to work with any randomly generated sequence.
If it works for noise it works for any deterministic signal, oder?
N=20;
A=randi([-10 10],1,N);
% nA=[1:1:numel(A)]
A
[B,reloc]=sort(A)
% dB=diff(B)
% B(find(dB~=0))
nv=0;
k=1
while k<=numel(B)-1
k=k+1
u0=B(k-1);u1=B(k);
if u0==u1
nv=[nv k]
k=k+1
while B(k-1)==B(k);
k=k+1
end
end
end
nv(1)=[]
B(nv)
for certain sequences there is an overflow message
Index exceeds matrix dimensions.
Error in find_long_bursts (line 17)
while B(k-1)==B(k);
don't worry, sequence
nv
contains the right indices and therefore
B(bv)
is ok.
Just had to do something else, I am cleaning it if Mr Honjas likes the code :) .
if you John BG's answer useful would you please mark it as Accepted Answer?
To any other reader, please if you find this answer of any help, click on the thumbs-up vote link,
thanks in advance for time and attention
John BG
  1 comentario
William Honjas
William Honjas el 19 de Dic. de 2016
Unbelievanlble how fast the community responds!! So appreciative. I am outside my office at the moment and see if the output is what I need tomorrow. I greatly appreciate the effort that you have put into this. Kinda restores my faith in humanity a bit! Thanks again!

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by