replace duplicate value by 0 in matrix or vector

17 Ansichten (letzte 30 Tage)
Mira le
Mira le am 15 Okt. 2019
Kommentiert: Mira le am 21 Okt. 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

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 15 Okt. 2019
Bearbeitet: Andrei Bobrov am 15 Okt. 2019
b = [1 2 1 3];
[a,c] = unique(b,'first');
out = zeros(size(b));
out(c) = a;

Weitere Antworten (2)

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

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

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by