mean value of subarrays
Show older comments
I have a 3-dimensinal array ( 1483 rows, 417 columns and 4 bands). I am trying to get a mean value for each sub-array of 92rows*92columns. I have some zero values that I want to exclude when the calcualtion of mean is being done. I used the following code but it's not correct.my data type is uint32. i apperciate your help.
Accepted Answer
More Answers (1)
Sean de Wolski
on 22 Apr 2011
Here's a 2-dimensional example that demonstrates the logic and kernels.
%sample data
A = uint32(repmat(magic(10),[4,1]));
A(A<10) = 0;
%We will use a window size 6x10
window = ones(6,10);
nnzeros = conv2(double(logical(A)),window,'valid'); %you can change this shape do whatever it is you want at the edges.
winsum = conv2(double(A),window,'valid');
mean_wout_zeros = winsum./nnzeros;
%check that it worked (for first valid value)
isequal(sum(sum(A(1:6,:)))/nnz(A(1:6,:)),mean_wout_zeros(1))
You could convert mean_wout_zeros back to uint32 if you want...
4 Comments
Hassan
on 22 Apr 2011
Sean de Wolski
on 22 Apr 2011
Where does the 16x5 come from? It seems to be appearing out of the air. For the skipping every 92; do EXACTLY what I have above (with a ones(92) window and extract every 92nd value. I.e. mean_wout_zeros = mean_wout_zeros(1:92:end);
Hassan
on 22 Apr 2011
Hassan
on 22 Apr 2011
Categories
Find more on Neighborhood and Block Processing 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!