Asked by Shubham Mohan Tatpalliwar
on 24 Oct 2018

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

Answer by Bruno Luong
on 24 Oct 2018

Accepted Answer

>> sum(A(:,1)+B==2)

ans =

1 0 2 1

>>

Shubham Mohan Tatpalliwar
on 24 Oct 2018

thanx a lot for that....

Shubham Mohan Tatpalliwar
on 25 Oct 2018

can u explain a bit about this code?

Bruno Luong
on 25 Oct 2018

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.

Sign in to comment.

Answer by Stephen Cobeldick
on 24 Oct 2018

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

Shubham Mohan Tatpalliwar
on 24 Oct 2018

Really this was very helpful for me

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## madhan ravi (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/425836-how-to-find-common-values-in-two-matrix-for-particular-column#comment_627391

## Bruno Luong (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/425836-how-to-find-common-values-in-two-matrix-for-particular-column#comment_627395

## Shubham Mohan Tatpalliwar (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/425836-how-to-find-common-values-in-two-matrix-for-particular-column#comment_627396

Sign in to comment.