MATLAB: Replace corresponding elements in two different matrices

arrayelementMATLABreplace

I have two matrices A and B. The matrix A contains missing data (shown as -9999) which I have replaced with NaN. Matrix B has no missing data. I want to replace the corresponding elements in B with NaN as well so the NaNs for both matrices are located in the same cells. How can I do that?

Best Answer

Try this:
A = [1 2 3 4; 5 NaN 7 8; 9 10 11 NaN]
B = randi(9, 3, 4)
NewB = B
NewB(isnan(A)) = NaN
producing:
A =
1 2 3 4
5 NaN 7 8
9 10 11 NaN
B =
2 7 6 8
5 8 3 4
7 7 4 1
NewB =
2 7 6 8
5 8 3 4
7 7 4 1
NewB =
2 7 6 8
5 NaN 3 4
7 7 4 NaN
Related Question