How to avoid NaN when comparing data
显示 更早的评论
Hello
I have a quite generalized question that has been troubling me. When comparing data such as examples with real, we sometimes have 0 or NaN values. As is the case, I thought to work around it in three ways:
1.I replaced the NaN with a number (e.g. 0)
2.Replace NaN with values interpolated scheme
3. Replace NaN with the next / or previous value
Although when faced with a very big series of real values i noticed that by interpolating or replacing the NaN my results are changed.
How can we compare the two vector for a process , for example their coefficient but with not considering the NaN values and their corresponding simulated value.
p.s. am aware of the available options in matlab such as nanmean which do not taken into account the nan values of a vector, I am wondering though how can you compare two vectors (values) and when you detect a NaN to skip the NaN value and the simulated/other value that is to be compared.
I try to solve it on an example I have done, trying the functionality
% generate a random valuable and insert NaN
A=rand(500,1)
b=randi(500,20,1)
A(b)=NaN
plot(A)
%generate a same size random variable to be compared with A
B=rand(500,1)
% Just visual confirmation of the location of the NaN's in A, by replaing
% them with 0
c=A
c(isnan(c))=0
c(c==0)=-1
plot(c)
% process of comparison
corrcoef(A,B)
The objective is where the NaN are located they are not taken into account and the corresponding B value that was to be compared is also ignored.
Thank you
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!