mean for matrix array
2 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
回答(2 个)
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.
0 个评论
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!