adding a condition to blkproc()

I use the following code to find the mean value of non-zero values of each block of an array. I wonder how I can add a condition to have a mean value if the number of non-zero values are bigger than 75% of the size of the block. I apperciate your help.
mean_wout_zeros = blkproc(Myarray,[block block],@(x)sum(x(:))./(sum(logical(x(:)))));

Respuestas (1)

Sean de Wolski
Sean de Wolski el 8 de Jul. de 2011

1 voto

Just use this as your function
function out = meanZ75(in)
%mean sans zeros if there are 25% or more zeros
%pure mean if there are < 25% zeros
%SCd 07/11/2011
%
in = in(:);
if nnz(in) >= 0.25*length(in);
out = mean(in(~~in));
else
out = mean(in);
end

4 comentarios

Hassan
Hassan el 9 de Jul. de 2011
Hi Sean, thanks for the comment. I dont know how to do that. could you please tell me in an example?
Ashish Uthama
Ashish Uthama el 11 de Jul. de 2011
Hassan, use meanZ75 as the input function handle: meanOut = blkproc(Myarray, [b b], @meanZ75);
Hassan
Hassan el 15 de Jul. de 2011
Thanks Sean for the code but I dont know how to use it. Should it be like as follows? what is 'in'?
mean_wout_zeros = blkproc(new_array2(:,:,i),[win_m win_m],@(x)sum(x(:))./(sum(logical(x(:)))));
function out = meanZ75(in)
%mean sans zeros if there are 25% or more zeros
%pure mean if there are < 25% zeros
%SCd 07/11/2011
%
in = in(:);
if nnz(in) >= 0.25*length(in);
out = mean(in(~~in));
else
out = mean(in);
end
Sean de Wolski
Sean de Wolski el 15 de Jul. de 2011
save
function out = meanZ75 etc.
to 'meanZ75.m' in your MATLAB folder in your documents. Then use it as the function for blkproc
blkoproc(new_array2(:,:,iu),[10 10],'meanZ75');

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 8 de Jul. de 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by