Matlab

Moderators: None (Apply to moderate this forum)
Number of threads: 1467
Number of posts: 2144

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
first time user syntax help Posted by goaddr on 10 May 2011 at 9:32 AM
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!


Report
Re: first time user syntax help Posted by sambo8 on 10 May 2011 at 11:47 PM
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.

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\n',block) %display in the command window the result  




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.