I have a multidimensional array how can I average every 60 rows and 12 columns?
Show older comments
I have an array of velocity U every 1/6 degree latitude and longitude over 42 depth levels, 2160x320x42 I want to average the data every 60rows and every 12 columns so that it is on a coarse grid of 10longigtude x 2latitude I also have arrays of latitude and Longitude XC 2160X320 and YC 2160X320.
1 Comment
Nagarjuna Manchineni
on 7 Mar 2017
Can you elaborate more on what you are trying to do, may be a sample MATLAB script or a sample input/output data would be great to understand your use-case.
Answers (1)
The number of columns 320 cannot be divided by 12. What should happen with the right most block?
x = rand(2160, 320, 42);
y = BlockMean(x, 60, 12);
Use either the M-version or if speed matters compile the C-Mex function.
Manually:
S = size(X);
M = S(1) - mod(S(1), V);
N = S(2) - mod(S(2), W);
MV = M / V;
NW = N / W;
XM = reshape(X(1:M, 1:N, :), V, MV, W, NW, []);
Y = sum(sum(XM, 1), 3) .* (1.0 / (V * W));
Y = squeeze(X);
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!