I have two columns, X and Y.
The X column is a series of consecutive numbers (1,2,3...). The Y column is my data.
I want to make Y=NaN at certain X values
for example, lets say: x= 1,2,3,4,5 and y= .4,.6,.2,.6,.9
I want to remove the y=.6 that corresponds to x=4 How do I do this?
Thanks!

 채택된 답변

David Fletcher
David Fletcher 2018년 3월 12일
편집: David Fletcher 2018년 3월 12일

0 개 추천

data=[1 0.4;2,0.6;3,0.2;4,0.6;5 0.9]
index=data(:,1)==4;
data(index,2)=NaN
Something like this?

댓글 수: 6

Devon Fisher-Chavez
Devon Fisher-Chavez 2018년 3월 12일
Yes, except my actual X and Y columns are extremely large and I dont want to combine them into one line.
I was hoping I could do something like "if X=... then Y= NaN" except in Matlab language
for a single value (ex. 4) I was able to say
y(x == 4) = NaN
But I can only do it for a single value of X. I'm a total novice haha
David Fletcher
David Fletcher 2018년 3월 12일
편집: David Fletcher 2018년 3월 12일
If you want to do it for multiple x values you could chain the clause together with logical or
y(x == 4|x==5|x==7) = NaN
it could become a bit cumbersome if you want to screen out a lot of x values though, so you could index with a vector of the scalers you want to screen out
y(any(x==[1 2 5 6 9])')=NaN
Devon Fisher-Chavez
Devon Fisher-Chavez 2018년 3월 12일
I tried "y(any(x==[1 2 5 6 9])')=NaN" but it says "matrix dimensions must agree"
David Fletcher
David Fletcher 2018년 3월 12일
편집: David Fletcher 2018년 3월 12일
Sorry, it's probably because I was working with a row vector rather than a column - transpose it [1 2 5 6 9]'
y(any(x==[1 2 3]')')=NaN
Devon Fisher-Chavez
Devon Fisher-Chavez 2018년 3월 12일
That works! thank you so much!
David Fletcher
David Fletcher 2018년 3월 12일
OK no probs

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 NaNs에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by