How to divide a 2d array into blocks and create an array with the mean value of each block?
9 views (last 30 days)
Show older comments
Here's an 4x8 array.Three of its values are NaNs:
B=zeros(4,8);
B(1,:) = [1 3 4 3 10 0 3 4];
B(2,:) = [5 7 2 NaN 8 2 3 4];
B(3,:) = [8 4 6 2 NaN 3 5 7];
B(4,:) = [2 2 5 5 1 NaN 4 4];
I want to divide the array into 2x2 blocks and form a new array 'C' that is the mean of each block. If the block contains one or more NaNs, I want to find the average of the numbers that are not NaNs. In this example, I would like the following output:
C =
4.0000 3.0000 5.0000 3.5000
4.0000 4.5000 2.0000 5.0000
I tried the code below but it just returns a NaN if any of the block elements are NaN. How should I amend this, please?
C=blockproc(B, [2 2], @(block_struct) mean(block_struct.data(:)))
C =
4.0000 NaN 5.0000 3.5000
4.0000 4.5000 NaN 5.0000
0 Comments
Accepted Answer
Jan
on 26 May 2022
Edited: Jan
on 26 May 2022
B = [1 3 4 3 10 0 3 4; ...
5 7 2 NaN 8 2 3 4; ...
8 4 6 2 NaN 3 5 7; ...
2 2 5 5 1 NaN 4 4];
B = repmat(reshape(B, 1, 4, 8), 10, 1, 1); % The test data
% Code without BLOCKPROC:
N = ~isnan(B); % Mask for the NaNs
S = size(B);
T = [S(1), 2, S(2)/2, 2, S(3)/2];
BB = reshape(B .* N, T); % Set NaNs to 0
NN = reshape(N, T);
R = sum(BB, [2, 4]) ./ sum(NN, [2, 4]); % Sum over 2x2 blocks
R = reshape(R, T([1,3,5])); % Remove singelton dimensions
2 Comments
Jan
on 27 May 2022
You are welcome. I do not have the image processing toolbox, so I use solutions without blockproc.
The method shown above is fast and more flexibel, if the inputs are no 2D or 3D RGB arrays and the blocks are built on the 1st 2 dmensions. Instead of ~isnan() other masks can be defined. blockproc is more powerful, when specific functions are applied to the blocks, but power costs processing time.
See also: https://www.mathworks.com/matlabcentral/fileexchange/24812-blockmean (no masking of NaNs impelemented, frist 2 dimensions processed also, dimensions do not need to be a multiple of the window size)
More Answers (1)
David Hill
on 26 May 2022
C=blockproc(B, [2 2], @(block_struct) mean(block_struct.data(:),'omitnan'))
2 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!