replace duplicate value by 0 in matrix or vector

4 visualizaciones (últimos 30 días)
Mira le
Mira le el 15 de Oct. de 2019
Comentada: Mira le el 21 de Oct. de 2019
How do we replace duplicate value by 0 in matrix or vector ?
for example
Input:
b = 1 2 1 3
Output::
b= 1 2 0 3
Thanks

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 15 de Oct. de 2019
Editada: Andrei Bobrov el 15 de Oct. de 2019
b = [1 2 1 3];
[a,c] = unique(b,'first');
out = zeros(size(b));
out(c) = a;

Más respuestas (2)

Adam
Adam el 15 de Oct. de 2019
[C,ia] = unique( b );
b( setdiff( 1:numel(b), ia ) ) = 0;

Jos (10584)
Jos (10584) el 15 de Oct. de 2019
% for small vectors:
b = [1 2 1 3 2 1 4 2]
b(sum(triu(b == b')) > 1) = 0

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by