Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Nested loop help

Subject: Nested loop help

From: patricia

Date: 18 Jul, 2011 19:05:15

Message: 1 of 4

Hi there

just wondering can anyone help me in forming this nested loop. I am trying to select regions of 3 by 3 starting at the first pixel and move along the image until it reaches the end and extracting features from each region. However at the moment i am getting a error reading:

??? Index exceeds matrix dimensions.

Error in ==> nested at 6
        R = A(r:r+2, c:c+2);

Can anyone help? This is the code i have below:

clear;
A=getImageFunction;

for c=size(A,2) % Number of columns
    for r=size(A,1); %Number of rows
        R = A(r:r+2, c:c+2);
        %Extract the features
        FM(regionNum, 1) = mean(mean(I));
        FM(regionNum, 2) = std2(I);
        regionNum = regionNum +1;
        
    end
    
end

Regions_Calculated=regionNum;

Subject: Nested loop help

From: Roger Stafford

Date: 18 Jul, 2011 20:13:08

Message: 2 of 4

"patricia " <tishi@live.co.uk> wrote in message <j0205b$f2k$1@newscl01ah.mathworks.com>...
> .........
> for c=size(A,2) % Number of columns
> for r=size(A,1); %Number of rows
> R = A(r:r+2, c:c+2);
> %Extract the features
> FM(regionNum, 1) = mean(mean(I));
> FM(regionNum, 2) = std2(I);
> regionNum = regionNum +1;
> end
> end
> .........
- - - - - - - - - -
  I see at least three problems here:

1. When c is equal to size(A,2) then A(~,c+1) and A(~,c+2) are outside its range. The same is true when r is equal to size(A,1).

2. The indices c and r are clearly intended to range starting from 1, but this code just uses the end value for each one.

  The problems in 1. and 2. would be corrected by writing:

 for c = 1:size(A,2)-2
  for r = 1:size(A,1)-2

3. After obtaining the 3 x 3 region in R, the subsequent code does not use R but references a matrix called 'I', so no use is made of R in the for-loops.

Roger Stafford

Subject: Nested loop help

From: patricia

Date: 19 Jul, 2011 10:27:28

Message: 3 of 4

"Roger Stafford" wrote in message <j0244j$rg1$1@newscl01ah.mathworks.com>...
> "patricia " <tishi@live.co.uk> wrote in message <j0205b$f2k$1@newscl01ah.mathworks.com>...
> > .........
> > for c=size(A,2) % Number of columns
> > for r=size(A,1); %Number of rows
> > R = A(r:r+2, c:c+2);
> > %Extract the features
> > FM(regionNum, 1) = mean(mean(I));
> > FM(regionNum, 2) = std2(I);
> > regionNum = regionNum +1;
> > end
> > end
> > .........
> - - - - - - - - - -
> I see at least three problems here:
>
> 1. When c is equal to size(A,2) then A(~,c+1) and A(~,c+2) are outside its range. The same is true when r is equal to size(A,1).
>
> 2. The indices c and r are clearly intended to range starting from 1, but this code just uses the end value for each one.
>
> The problems in 1. and 2. would be corrected by writing:
>
> for c = 1:size(A,2)-2
> for r = 1:size(A,1)-2
>
> 3. After obtaining the 3 x 3 region in R, the subsequent code does not use R but references a matrix called 'I', so no use is made of R in the for-loops.
>
> Roger Stafford

Hi Roger

Thanks for your help, i am newto matlab and just trying to get my head round it all. I have changed the start of the for loop to what you suggested. so my code is now the following, so im i right in thinking this is now calculating the features for the region?

clear;
A=getImageFunction;
regionNum=1;

for c = 1:size(A,2); % Number of columns
  for r = 1:size(A,1); %Number of rows
       R = A(r:r-2, c:c-2);
        %Extract the features
        FM(regionNum, 1) = mean(mean(A));
        FM(regionNum, 2) = std2(A);
        regionNum = regionNum +1;
        
    end
    
end

Regions_Calculated=regionNum;

Subject: Nested loop help

From: Roger Stafford

Date: 19 Jul, 2011 13:25:29

Message: 4 of 4

"patricia " <tishi@live.co.uk> wrote in message <j03m6g$l9$1@newscl01ah.mathworks.com>...
> ........
> for c = 1:size(A,2); % Number of columns
> for r = 1:size(A,1); %Number of rows
> R = A(r:r-2, c:c-2);
> %Extract the features
> FM(regionNum, 1) = mean(mean(A));
> FM(regionNum, 2) = std2(A);
> regionNum = regionNum +1;
> end
> end
> .........
- - - - - - - - -
  No, this code still won't work for roughly the same reasons as before.

  When c starts at c = 1, then the value of c-2 in c:c-2 would be 1-2 = -1 which is not allowed as an index value.

  Also the indices in r:r-2 and c:c-2 won't automatically go downward unless you put a minus one in between: r:-1:r-2. If you just write a:b, the default increment size is assumed to be +1. For any other increment size, you need to specify it with a middle value, as in a:d:b.

  Actually I have already shown you how to do the indexing for c and r.

  Finally, what point is there in computing R if you are not going to use it for something? As it stands, you are placing the same features in FM over and over again, because A doesn't change in your code.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us