MATLAB Answers

0

replace duplicate value by 0 in matrix or vector

Asked by Mira le on 15 Oct 2019
Latest activity Commented on by Mira le on 21 Oct 2019 at 7:29
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

  0 Comments

Sign in to comment.

Products

3 Answers

Answer by Andrei Bobrov
on 15 Oct 2019
Edited by Andrei Bobrov
on 15 Oct 2019
 Accepted Answer

b = [1 2 1 3];
[a,c] = unique(b,'first');
out = zeros(size(b));
out(c) = a;

  1 Comment

thank you

Sign in to comment.


Answer by Adam
on 15 Oct 2019

[C,ia] = unique( b );
b( setdiff( 1:numel(b), ia ) ) = 0;

  0 Comments

Sign in to comment.


Answer by Jos (10584)
on 15 Oct 2019

% for small vectors:
b = [1 2 1 3 2 1 4 2]
b(sum(triu(b == b')) > 1) = 0

  0 Comments

Sign in to comment.