MATLAB: Iterating through each element in a matrix

indexingMATLAB

Hello,
Below is my code i want check if elements in vector/matrix match the other vector/matrix/scalar and popullate a empty matrix with the common elements. Attaching my code. In the attached code i have two Row Vectors of same size but want to know if it any different if one of them is scalar or (mxn) matrix and other is Row or Column Vector.
A = [1 2 3 2 5 2 5];
B = [3 3 3 3 39 9 9];
s = zeros(size(A));
for i = 1:size(A)
if any(A(i)==B(i))
s = s(i);
end
end

Best Answer

s = intersect(A, B);