Effacer les filtres
Effacer les filtres

How to ignore nan values in using trapz function

129 vues (au cours des 30 derniers jours)
Bijay Guha
Bijay Guha le 13 Fév 2018
Modifié(e) : Luis le 1 Avr 2024
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

Réponse acceptée

Birdman
Birdman le 13 Fév 2018
Modifié(e) : Birdman le 13 Fév 2018
Before evaluation, you can get rid of nans as follows:
height=height(~isnan(height))
data=data(~isnan(data))
  7 commentaires
Torsten
Torsten le 18 Déc 2023
Modifié(e) : Torsten le 18 Déc 2023
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 le 1 Avr 2024
Modifié(e) : Luis le 1 Avr 2024
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.)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by