MATLAB: Matrix manipulation syntax help

matrix

Hey, I have a 2048×2048 matrix with values of either 0 or 1 which is called "white". I need to find the total number of 3×3 blocks in the matrix where each data point has a value of 1, ie,
1 1 1
1 1 1
1 1 1
Im a first time user and I think that my problem is syntax. My code is as follows,
x=0
for i=2:2047 j=2:2047
if white(i-1,j-1)==1 & white(i-1,j)==1 & white(i-1,j+1)==1 & white(i,j-1)==1 & white(i,j)==1 & white(i,j+1)==1 & white(i+1,j-1)==1 & white(i+1,j)==1 & white(i+1,j+1)==1
x=x+1
end
y=sum(x)
end
Ive been at it for a few hours and any help would be really appreciated. Ill be spending the rest of the afternoon trying to solve this problem….
Thanks for your help!

Best Answer

You could do this with your for-loop like this:
n = 0;
for ii = 2:2047
for jj = 2:2047
if(all(all(white(ii-1:ii+1,jj-1:jj+1))))
n = n+1;
end
end
end
Related Question