How to define values in a vector as NaN ?

1 view (last 30 days)
Hello,
I have a variable A that is 2x3 (x and y values) and variable B that is 2x2 (also x and y values). B actually corresponds to some of the x,y pairs in A. I would like to replace the pairs in A that correspond to those in B by NaN.
A = [1 2 3; 4 5 6];
Ax = A(1,:);
Ay = A(2,:);
B = [2 3; 5 6];
Bx = B(1,:);
By = B(2,:);
Ay(By) = NaN %this is multiplying instead of redefining --> not what I want
Ax(By) = NaN %same here
A = [Ay;Ax]
My goal is to have A = [1 NaN NaN; 4 NaN NaN].
Can anyone help please ?

Accepted Answer

Star Strider
Star Strider on 26 Nov 2021
Try this (using ismember) —
A = [1 2 3; 4 5 6];
B = [2 3; 5 6];
C = ismember(A,B)
C = 2×3 logical array
0 1 1 0 1 1
A(C) = NaN
A = 2×3
1 NaN NaN 4 NaN NaN
.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!