How to average finite components only of 4 data?
4 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Stephan
on 20 Mar 2019
Hi,
% 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
0 Comments
More Answers (0)
See Also
Categories
Find more on Oceanography and Hydrology in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!