フィルターのクリア

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

3 ビュー (過去 30 日間)
Ideth Kara
Ideth Kara 2020 年 9 月 22 日
コメント済み: Image Analyst 2020 年 9 月 22 日
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
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
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 件のコメント
Ideth Kara
Ideth Kara 2020 年 9 月 22 日
Great! thank you so much
Ameer Hamza
Ameer Hamza 2020 年 9 月 22 日
I am glad to be of help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by