Q:

How to get a matrix that has the common elements of two matrices?

Suppose I have two matrices, A and B.
A = [2 8 4; 7 3 9];
B = [2 1 6; 1 3 9];

I'd like to get a matrix C that is as follows:
C = [2 0 0; 0 3 9];

C is a matrix that retains the common elements of A and B but changes the rest of the elements to zero. I could use a for loop and iterate over every element in both A and B but is there a more efficient method to obtain the results?

A:

Assuming both matrices have same dimensions. 
A = [2 8 4; 7 3 9];
B = [2 1 6; 1 3 9];
C = zeros(size(A));

C(A == B) = A(A == B);

C =

     2     0     0
     0     3     9

