Vector matrix multiplication with a condition

7 ビュー (過去 30 日間)
Madhubhashi Bamunuarachchi
Madhubhashi Bamunuarachchi 2019 年 1 月 30 日
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 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