Add two binary matrices and get only "1" in each row
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Image Analyst
2020 年 9 月 22 日
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!!!
採用された回答
Ameer Hamza
2020 年 9 月 22 日
編集済み: Ameer Hamza
2020 年 9 月 22 日
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!