how to find common values in two matrix for particular column?
조회 수: 1 (최근 30일)
이전 댓글 표시
if true
% code
A=[0 1 1 0; 1 0 0 1; 1 1 0 0; 0 0 1 1]
B=[0 1 0 1; 1 0 1 0; 0 0 1 1; 1 1 0 0]
end
for 1st column of A and ALL columns of b
if we check
the expected answer is
1 0 2 1
these are the total number of instances they are matching
댓글 수: 3
Bruno Luong
2018년 10월 24일
Please put care on
- code formatting
- explanation
- formulation of synthetic question (Why give the whole A and ask just result that depends only on first column)
채택된 답변
Bruno Luong
2018년 10월 24일
>> sum(A(:,1)+B==2)
ans =
1 0 2 1
>>
댓글 수: 9
Bruno Luong
2018년 10월 25일
편집: Bruno Luong
2018년 10월 25일
The reshape() just moves the 2nd dimension (column) of A to the 3rd dimension
So each origin column A(:,j) now can be addressed as A(:,:j).
The explanation for SUM(... & B) you already know, but now use in the context of auto-expansion. Excepted that the result now is of the size (1 x size(B,2) x size(A,2)): each number of common 1-values of B and A(:,j) is in a slide XX(1,:,j) before SQUEEZE is invoked.
The squeeze command removes the 1st singleton dimension, so XX(:,j) is common 1-values of B and A(:,j).
NOTE: You might transpose the result so each row corresponds to result of a column of A with you prefer.
추가 답변 (1개)
Stephen23
2018년 10월 24일
You don't need to use a loop:
>> sum(permute(A,[3,2,1])&permute(B,[2,3,1]),3)
ans =
1 0 1 2
0 1 2 1
2 1 0 1
1 2 1 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!