Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
ahmad
ahmad el 24 de Mayo de 2022
Respondida: KSSV el 24 de Mayo de 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

Respuesta aceptada

KSSV
KSSV el 24 de Mayo de 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

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by