How to define values in a vector as NaN ?

조회 수: 2 (최근 30일)
meryem berrada
meryem berrada 2021년 11월 26일
댓글: Star Strider 2021년 11월 26일
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 ?

채택된 답변

Star Strider
Star Strider 2021년 11월 26일
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
meryem berrada
meryem berrada 2021년 11월 26일
That was perfect, thank you so much !
Star Strider
Star Strider 2021년 11월 26일
As always, my pleasure!
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by