How to ignore nan values in using trapz function

Hello.. I have two column matrix one is height another one is data, both are of same sizes. But both includes some NaN values.
height=[NaN;NaN;NaN;0.246;1.252;2.253;3.2470;4.228;5.192;6.139;7.072];
data=[NaN;NaN;NaN;NaN;NaN;0.0014428;0.0018342;0.0019822;0.0017613;0.0013172;NaN];
I want to integrate the data with respect to height, but as expected it gives NaN output, as the data contains NaN values. Actually the number of NaN is varying for different sets. So can anyone help in general way to omit this NaN element in using trapz function (just like nanmean, nansum, nanstd etc). Extrapolation will give error in my case, so I need answer without extrapolation. Any answer will be helpful. Thanks in advance

 採用された回答

Birdman
Birdman 2018 年 2 月 13 日
編集済み: Birdman 2018 年 2 月 13 日

1 投票

Before evaluation, you can get rid of nans as follows:
height=height(~isnan(height))
data=data(~isnan(data))

7 件のコメント

Bijay Guha
Bijay Guha 2018 年 2 月 13 日
as you can see in the example, it will definitely remove the nans, but will disrupts the order of remaining. Means 0.246 height level corresponds NaN value of data, but after doing the above mentioned ops the 1st row contains 0.246 and 0.0014428, in addition the length of them will also not be the same hence can't perform trapz.
Birdman
Birdman 2018 年 2 月 13 日
Try this idea:
height=[NaN;NaN;NaN;0.246;1.252;2.253;3.2470;4.228;5.192;6.139;7.072].'
data=[NaN;NaN;NaN;NaN;NaN;0.0014428;0.0018342;0.0019822;0.0017613;0.0013172;NaN].'
if sum(isnan(data))>sum(isnan(height))
heightNew=height(~isnan(height));
dataNew=data(~isnan(height));
idx=find(~isnan(dataNew));
dataNew(isnan(dataNew))=dataNew(idx(1));
elseif sum(isnan(height))>sum(isnan(data))
dataNew=data(~isnan(data));
heightNew=height(~isnan(data));
idx=find(~isnan(heightNew));
heightNew(isnan(heightNew))=heightNew(idx(1));
else
heightNew=height(~isnan(height));
dataNew=data(~isnan(data));
end
trapz(dataNew,heightNew)
Bijay Guha
Bijay Guha 2018 年 2 月 13 日
編集済み: Bijay Guha 2018 年 2 月 13 日
Thanks for your idea. But, it may end up adding many extra data sometimes which will be erroneous in this case. The organization of height according to the availability of data will be helpful rather. So I built up this little code with your idea, which works fine in my case.
dataNew=data(~isnan(data));
idx=find(~isnan(data));
heightNew=height(idx);
if(isnan(heightNew)==false)
I=trapz(dataNew,heightNew);
else
heightNew1=heightNew(~isnan(heightNew));
idx1=find(~isnan(heightNew));
dataNew1=dataNew(idx1);
I=trapz(dataNew1,heightNew1);
end
Thanks again..
Birdman
Birdman 2018 年 2 月 13 日
You are welcome. If my answer helped you can accept it so that others will know that this question has been solved.
hxen
hxen 2023 年 12 月 18 日
This was helpful. Hopefully a future version of Matlab should have an 'omitnan' built in option like for mean.
Torsten
Torsten 2023 年 12 月 18 日
編集済み: Torsten 2023 年 12 月 18 日
It's not possible that each function takes care about imported NaN values. You are the one who should "omitnan" whereever possible in your computations.
Luis
Luis 2024 年 4 月 1 日
編集済み: Luis 2024 年 4 月 1 日
Even the following is still wrong, as it adds erroneous area where the ~nan segments are joined together:
dataNans = isnan(Data); heightNans = isnan(Height);
useIndicees = ~dataNans & ~heightNans
trapz(Data(useIndicees),Height(useIndicees))
(Consider e.g. integrating the area of a wall where x=data is along width, and nan's in y=height signify gaps in the wall. I won't show my ugly code here to take care of that, but may be you can come up with something elegant.)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2018 年 2 月 13 日

編集済み:

2024 年 4 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by