Add two binary matrices and get only "1" in each row

2 visualizaciones (últimos 30 días)
Ideth Kara
Ideth Kara el 22 de Sept. de 2020
Comentada: Image Analyst el 22 de Sept. de 2020
Hello eveyone!
I have two binary matrices(m*n),i want to add those two matrices but in order to get only '1' in each row .
this is the output matrix ,for example in row (2,5,6 and 7) i have two '1' ,there is any solution to eliminate one of them ?
0 0 0 0 0
0 0 1 1 0
0 0 0 1 0
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
1 1 0 0 0
1 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Would you please help me ?
  1 comentario
Image Analyst
Image Analyst el 22 de Sept. de 2020
For the case (like rows 2 and 7) where the binary/logical matrices have 1's in different columns, which of the two 1's do you want to keep?
And are your matrices of class logical? Or class double? Or some integer class? It makes a difference!!!

Iniciar sesión para comentar.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 22 de Sept. de 2020
Editada: Ameer Hamza el 22 de Sept. de 2020
This is one way
M = [ ...
0 0 0 0 0
0 0 1 1 0
0 0 0 1 0
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
1 1 0 0 0
1 0 0 0 0
0 0 0 0 0
0 0 0 0 0];
M_out = M*0;
[v, c] = max(M, [], 2);
idx = sub2ind(size(M), find(v), c(v==1));
M_out(idx) = 1;
Result
>> M_out
M_out =
0 0 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 0
0 0 1 0 0
0 1 0 0 0
1 0 0 0 0
1 0 0 0 0
0 0 0 0 0
0 0 0 0 0
  2 comentarios
Ideth Kara
Ideth Kara el 22 de Sept. de 2020
Great! thank you so much
Ameer Hamza
Ameer Hamza el 22 de Sept. de 2020
I am glad to be of help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by