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 problem

Subject: Nested loop problem

From: patricia

Date: 19 Jul, 2011 12:41:26

Message: 1 of 2

Can anyone help me as im getting an error saying this line: R = A(r:r(L-1), c:c(L-1));
is exceeding the matrix dimensions. I am new at matlab and i am constructing a nested loop that will slide a 3by3 window through the input image and for each 3by3 region calculate the features mean and standard deviation, any help at all would be appreciated please


clear;
A=getImageFunction;

L=length(A);

%Set region number to 1
regionNum=1;

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

Subject: Nested loop problem

From: Steven_Lord

Date: 19 Jul, 2011 13:24:48

Message: 2 of 2



"patricia " <tishi@live.co.uk> wrote in message
news:j03u1m$l7h$1@newscl01ah.mathworks.com...
> Can anyone help me as im getting an error saying this line: R =
> A(r:r(L-1), c:c(L-1));
> is exceeding the matrix dimensions. I am new at matlab and i am
> constructing a nested loop that will slide a 3by3 window through the input
> image and for each 3by3 region calculate the features mean and standard
> deviation, any help at all would be appreciated please
>
>
> clear;
> A=getImageFunction;
>
> L=length(A);
>
> %Set region number to 1
> regionNum=1;
>
> for c = 1:size(A,2); % Number of columns
> for r = 1:size(A,1); %Number of rows
> R = A(r:r(L-1), c:c(L-1));

This command:

r:r(L-1)

attempts to retrieve the (L-1)st element of r and use that to construct a
vector using COLON. But r is a scalar (a 1-by-1) because of the way you've
written your code, so if L is anything other than 2 this will not work.

If you have Image Processing Toolbox, take a look at the BLOCKPROC function:

http://www.mathworks.com/help/toolbox/images/f7-12726.html

or NLFILTER:

http://www.mathworks.com/help/toolbox/images/f7-12915.html

If not, you may want to create vectors containing the indices of the first
row/column for each block:

firstColumn = 1:3:size(A,2);
firstRow = 1:3:size(A, 1);

Now the nth block goes from column firstColumn(n) to firstColumn(n+1)-1 and
similarly for the rows. [I'll leave you to put this into your framework and
figure out how to handle the case where the number of rows/columns in your
matrix is not evenly divisible by 3.]

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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