# Average values with nan value

7 Ansichten (letzte 30 Tage)
Kommentiert: Adam Danz am 22 Mär. 2023
Hi
I have a matrix with 3 dimentions; i,j,t(time).
I try to calculate mean value for each time step but the matter is we have some nan-values. I have four basins and decide to calculate avrage for each basin seperately. It doesnot work properly
............................................
for t= 1:length(time)
if j>85
U1(t)=nanmean(Uwind,'all')
else if j>65 & j<85
U2(t)=nanmean(Uwind,'all')
else if j>25 & j<65
U3(t)=nanmean(Uwind,'all')
else
U4(t)=nanmean(Uwind,'all')
end
end
end
end
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Antoni Garcia-Herreros am 16 Mär. 2023
Not exacty sure what you mean by U1(t). I you want to calculate the mean (excluding NaNs) for each timestep for all i and a specified range of j this should work.
U4=nanmean(Uwind(:,1:25,:),[1,2]);
U3=nanmean(Uwind(:,26:64,:),[1,2]);
U2=nanmean(Uwind(:,65:84,:),[1,2]);
U1=nanmean(Uwind(:,65:end,:),[1,2]);
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Adam Danz am 22 Mär. 2023
Instead of nanmean, use mean with the omitnan flag.
For example,
U4 = mean(Uwind(:,1:25,:),[1,2],'omitnan');
mean has more capabilities such as supporting tall arrays, GPU arrays, and code generation, works better with tables, etc. nanmean is discouraged starting in MATLAB R2020b.

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Cameron am 16 Mär. 2023
myArray = nan(8,1); %array of nan values
indx = 1:2:7; %index to populate that array
myArray(indx,1) = rand(length(indx),1) %have some nan and some numbers
myArray = 8×1
0.1698 NaN 0.0233 NaN 0.8454 NaN 0.6111 NaN
mean(myArray(~isnan(myArray))) %take the average of only the numbers
ans = 0.4124
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu ROI-Based Processing finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by