Vector matrix multiplication with a condition

조회 수: 5 (최근 30일)
Madhubhashi Bamunuarachchi
Madhubhashi Bamunuarachchi 2019년 1월 30일
댓글: Madhubhashi Bamunuarachchi 2019년 1월 31일
Hi,
I need to find which rows of A are equal to B and result should be a vector with 1's and 0's (1 for rows that are equal and zero for not equal).
However, A's positions with value 2 should be disregarded in comparison and should not affect the equality check.
A = 1 1 2 0
1 0 1 2
2 2 1 0
B = 1 0 1 1
Can this be done without a for loop as following?
[row,col] = size(A);
for i = 1:row
c(i) = all(A(i,:) == B | A(i,:) == 2);
end
Thank you.

채택된 답변

James Tursa
James Tursa 2019년 1월 31일
For later versions of MATLAB:
c = all(A==B | A==2,2);
For earlier versions:
c = all(bsxfun(@eq,A,B) | A==2,2);

추가 답변 (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