Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
ahmad
ahmad le 24 Mai 2022
Réponse apportée : KSSV le 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

Réponse acceptée

KSSV
KSSV le 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

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by