Effacer les filtres
Effacer les filtres

How to average finite components only of 4 data?

1 vue (au cours des 30 derniers jours)
Yandong Lang
Yandong Lang le 20 Mar 2019
My question is that I have four 3D ocean data(360*160*50). I want to average the finite numbers of the 4 data. For example:
data1(1,2,3) = NaN;
data2(1,2,3) = 3;
data3(1,2,3) = NaN;
data4(1,2,3) =4
In this case, I will sum data2(1,2,3) and data4(1,2,3) and then dividid by 2. How to do it for the whole data?
It needs to be taken into account that continent area of the 4 data are all NaN because it is ocean data. Due to this, I can not replace all the NaN to be zero.

Réponse acceptée

Stephan
Stephan le 20 Mar 2019
Hi,
the mean function has the option nanflag. That makes life easy:
% create example data containing NaN values
data1 = randi(100,5,4,3);
data2 = randi(100,5,4,3);
data3 = randi(100,5,4,3);
data1(randperm(60,15))= NaN;
data2(randperm(60,15))= NaN;
data3(randperm(60,15))= NaN;
% calculate the average ignoring NaN-data
result = mean(reshape(vertcat(data1,data2,data3),[],1),'omitnan')
Since the first 6 rows are just for the example you can do it in one line of code.
Best regards
Stephan

Plus de réponses (0)

Catégories

En savoir plus sur Oceanography and Hydrology 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