replace duplicate value by 0 in matrix or vector

2 次查看(过去 30 天)
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

采纳的回答

Andrei Bobrov
Andrei Bobrov 2019-10-15
编辑:Andrei Bobrov 2019-10-15
b = [1 2 1 3];
[a,c] = unique(b,'first');
out = zeros(size(b));
out(c) = a;

更多回答(2 个)

Adam
Adam 2019-10-15
[C,ia] = unique( b );
b( setdiff( 1:numel(b), ia ) ) = 0;

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

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by