first time user syntax help
Hey, I have a 2048x2048 matrix with values of either 0 or 1 which is called "white". I need to find the total number of 3x3 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!
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!
0
Comments
for j=1:10
things = do(stuff);
end
end[/php]As an aside, 'x' is only a single element so y = sum(x) will always be equal to x.
2:2, 3:3, 4:4 etc.... You'd miss situations like 2:3, 2:4 etc....
That's why you need to do it in 2 loops just from a basic logical reason.
Also if you have a grid thats is 2048x2048 you don't need to do a loop that goes from 1-2048 you only need to loop from 1-2046 because if you are checking a 3x3 grid then you don't need to do a check when you are on the 2nd from the last outer edges.