How to define values in a vector as NaN ?

2 visualizzazioni (ultimi 30 giorni)
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 ?

Risposta accettata

Star Strider
Star Strider il 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
.
  2 Commenti
meryem berrada
meryem berrada il 26 Nov 2021
That was perfect, thank you so much !
Star Strider
Star Strider il 26 Nov 2021
As always, my pleasure!
.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by