first time user syntax help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

first time user syntax help

goaddrgoaddr Posts: 1Member
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!


Comments

  • sambo8sambo8 Posts: 3Member
    Well I don't know how the matrix white is generated but I have a code for you. I took the example of a 6x6 matrix with two blocks and it works fine. Here's the code, but in your case, you should replace 3(6-3) by 2045(2048-3) in the for loops.

    [code]white = [1 1 1 0 0 0; 1 1 1 0 0 0; 1 1 1 0 0 0; 0 0 0 1 1 1; 0 0 0 1 1 1; 0 0 0 1 1 1];

    % 1 1 1 0 0 0
    % 1 1 1 0 0 0
    % 1 1 1 0 0 0
    % 0 0 0 1 1 1
    % 0 0 0 1 1 1
    % 0 0 0 1 1 1

    block=0;
    for i=0:3

    for j=0:3

    b=1;
    count=0;

    while(b<=3)
    c=1;
    while(c<=3)
    if white(i+b,j+c)==1,
    count = count+1; % check if there are nine consecutive ones in three colums and lines and sets count to nine
    end

    if (count==9),
    block = block+1; % if there are nine consecutive ones, count one block
    end

    c=c+1;
    end


    b=b+1;
    end

    clear count;
    end
    end


    fprintf('The total number of blocks is: %d
    ',block) %display in the command window the result [/code]
Sign In or Register to comment.