How to divide a 2d array into blocks and create an array with the mean value of each block?
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (1)
David Hill
am 26 Mai 2022
C=blockproc(B, [2 2], @(block_struct) mean(block_struct.data(:),'omitnan'))
Kategorien
Mehr zu Neighborhood and Block Processing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!