Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
Ideth Kara
Ideth Kara am 22 Sep. 2020
Kommentiert: Image Analyst am 22 Sep. 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 Kommentar
Image Analyst
Image Analyst am 22 Sep. 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!!!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 22 Sep. 2020
Bearbeitet: Ameer Hamza am 22 Sep. 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

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by