필터 지우기
필터 지우기

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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by