mean for matrix array

1 visualizzazione (ultimi 30 giorni)
Sophia
Sophia il 31 Mar 2016
Risposto: Chad Greene il 31 Mar 2016
I have a matrix t_u(361,361,36) i want to calculate the mean for 6 files in a row.. the resulting matrix should look like 361*361*6
for i = 1 : 6 :36
t_uavg(:,:,ceil(i / 6)) = mean(t_u(:,:,i : i + 5),3);
end
Is there any error in this code, it looks its working fine, the result is 361*361*6 but when i am plotting its not showing up anything for (:,:,4),(:,:,5) and (:,:,6)

Risposte (2)

the cyclist
the cyclist il 31 Mar 2016
I'm guessing the issue is with your t_u array. This code ...
t_u = rand(361,361,36);
t_uavg = zeros(361,361,6);
for i = 1 : 6 : 36
t_uavg(:,:,ceil(i / 6)) = mean(t_u(:,:,i : i + 5),3);
end
any(t_uavg(:)==0)
does not end up with any zeros in t_uavg, so I think your loop is right.

Chad Greene
Chad Greene il 31 Mar 2016
I tested your code with t_u = rand(361,361,36) and it seems to give values for all 6 slices. Do you have some NaNs in t_u that might be screwing up the mean?
Two notes that are unrelated to your problem:
1. Try to avoid using i or j as variables. Matlab thinks i = sqrt(-1) unless you overwrite it. It's usually not an issue, but debugging can be a headache when you try to use i without defining it.
2. Before the loop, always preallocate. This can be a big issue for large datasets. Preallocate by putting this before the loop:
t_uavg = NaN(361,361,6);

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by