MATLAB: How can I use the function on 3*3 blocks

blockprocImage Processing Toolbox

hi I have a matrix ,I wanna use a function on 3*3 blocks. my function is: if in a block we watch just a one this block should return 1 but if all 9 elements of this block is zero this function should return 0 how can I do this in matlab?
…all the elements of this matrix is zero and one.

Best Answer

inputData=zeros(10,10);
inputData(randi(100,[15,1]))=1
inputData =
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 1 0 0 0 0 0 0
% this is what you are looking for
result=blockproc(inputData,[3,3],@(block) any(block.data(:)~=0))
result =
0 0 1 0
1 0 1 1
1 0 1 0
1 1 0 0
% and if you want both inputData and result matrix be the same size do this:
result=blockproc(inputData,[3,3],@(block) any(block.data(:)~=0)*ones(3,3))
result =
0 0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 1 1 0 0 0
1 1 1 0 0 0 1 1 1 1 1 1
1 1 1 0 0 0 1 1 1 1 1 1
1 1 1 0 0 0 1 1 1 1 1 1
1 1 1 0 0 0 1 1 1 0 0 0
1 1 1 0 0 0 1 1 1 0 0 0
1 1 1 0 0 0 1 1 1 0 0 0
1 1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 0 0 0 0 0 0