How to replace duplicate element to 0 in matrix for every rows

2 次查看(过去 30 天)
I need to replace the repeated elements in column of a matrix as 0's, If my matrix is like this means.
Input =
1 1 1 2 2 2 3 3 4 4 5 5 5
1 2 2 3 3 3 4 4 4 5 5 6 6
1 1 1 1 2 2 3 4 5 5 5 6 6
My expected output should be like this
Output =
1 0 0 2 0 0 3 0 4 0 5 0 0
1 2 0 3 0 0 4 0 0 5 0 6 0
1 0 0 0 2 0 3 4 5 0 0 6 0

采纳的回答

KSSV
KSSV 2022-5-24
A = [1 1 1 2 2 2 3 3 4 4 5 5 5
1 2 2 3 3 3 4 4 4 5 5 6 6
1 1 1 1 2 2 3 4 5 5 5 6 6] ;
B = zeros(size(A)) ;
for i = 1:size(A,1)
[c,ia,ib] = unique(A(i,:)) ;
B(i,ia) = c ;
end
B
B = 3×13
1 0 0 2 0 0 3 0 4 0 5 0 0 1 2 0 3 0 0 4 0 0 5 0 6 0 1 0 0 0 2 0 3 4 5 0 0 6 0

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by