MATLAB: Replace equal values of two matrices with other values

MATLABmatrices

Hi,
I got following example matrices:
A = [0 8 29; 131 15 98; 25 827 9; 150 88 826];
B = [131 0; 827 1; 826 3];
I'd like to achieve following result:
C = [0 8 29; 0 15 98; 25 1 9; 150 88 3];
So I would like to replace the equal values of A and the first column of B with the values of the second column of B
My guess is ismember but I couldn't figure out a working solution

Best Answer

[ispresent, idx] = ismember(A, B(:,1));
C = A;
C(ispresent) = B(idx(ispresent), 2);