mean for matrix array

조회 수: 2 (최근 30일)
Sophia
Sophia 2016년 3월 31일
답변: Chad Greene 2016년 3월 31일
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)

답변 (2개)

the cyclist
the cyclist 2016년 3월 31일
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 2016년 3월 31일
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);

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by