mean for matrix array

2 Ansichten (letzte 30 Tage)
Sophia
Sophia am 31 Mär. 2016
Beantwortet: Chad Greene am 31 Mär. 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)

Antworten (2)

the cyclist
the cyclist am 31 Mär. 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 am 31 Mär. 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);

Kategorien

Mehr zu Loops and Conditional Statements 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