フィルターのクリア

mean for matrix array

1 回表示 (過去 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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by