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

2 Ansichten (letzte 30 Tage)
ahmad
ahmad am 24 Mai 2022
Beantwortet: KSSV am 24 Mai 2022
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

Akzeptierte Antwort

KSSV
KSSV am 24 Mai 2022
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

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by