How to avoid the multiple use of 'isnan' t

2019-09-18 22:35发布

I have two corresponded (has a relation and has the same dimension) dataset:

  1. Time
  2. Salinity

Some data in salinity dataset is NaN.

I can remove the NaN value by:

 Salinity_new=Salinity(~isnan(Salinity))

But it will not correspond to the Time dataset any more.

How can I remove the corresponded time in also?

Thanks

2条回答
▲ chillily
2楼-- · 2019-09-18 23:11

Another solution is the following:

indexes = find(isnan(Salinity)==1);
Salinity(indexes) = []; 
Time(indexes) = []

In this way you eliminate the non-numerical value from your vectors.

查看更多
神经病院院长
3楼-- · 2019-09-18 23:20

The comments of Diavakar and patrik are correct. To sum it up and get this question answered, some further remarks.

mask = isfinite(Salinity) 
[Time,Salinity] = deal( Time(mask), Salinity(mask) )

isfinite is the same as ~isnan - but with one computation step less, its about 50-90% faster. By introducing a mask you're avoiding the double use of isfinite. deal just saves you some space.

查看更多
登录 后发表回答