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:
for-loop

Subject: for-loop

From: diana van dijk

Date: 11 Jan, 2011 11:29:05

Message: 1 of 2

Hi,

I'm optimizing based on 2 state variables (x,y). As for x, I want to iterate over all values in the state space. As for y, I want to iterate over all values below x/(1-a), where 'a' is a parameter.
So far, I've coded this with an if-loop. But then I still iterate over all y's, with the consequence that I need to specify and 'else'. What I would like, is to never iterate over y > x/(1-a).
If I didn't need to coordinates of x and y in the 2D matrix M, I could have done 'for y=Y(Y<=x/(1-a))'. Is there an equivalent for my case where I do need to refer to the coordinates?
I hope you can help me!

X = 0.1:0.1:1
Y = 0.1:0.1:2

for x = 1:length(X)
for y = 1:length(Y)
if (y <= x/(1-a))
LB=...
UB=...
objfcn = @......name(x,y)
[result,fval,exitflag,output] = fminbnd( objfcn,LB,UB)
M(x,y) = result
else M(x,y) = 0
end
end

 

Subject: for-loop

From: David Young

Date: 11 Jan, 2011 12:25:08

Message: 2 of 2

"diana van dijk" wrote in message <igheu1$kdd$1@fred.mathworks.com>...
> Hi,
>
> I'm optimizing based on 2 state variables (x,y). As for x, I want to iterate over all values in the state space. As for y, I want to iterate over all values below x/(1-a), where 'a' is a parameter.
> So far, I've coded this with an if-loop. But then I still iterate over all y's, with the consequence that I need to specify and 'else'. What I would like, is to never iterate over y > x/(1-a).
> If I didn't need to coordinates of x and y in the 2D matrix M, I could have done 'for y=Y(Y<=x/(1-a))'. Is there an equivalent for my case where I do need to refer to the coordinates?
> I hope you can help me!
>
> X = 0.1:0.1:1
> Y = 0.1:0.1:2
>
> for x = 1:length(X)
> for y = 1:length(Y)
> if (y <= x/(1-a))
> LB=...
> UB=...
> objfcn = @......name(x,y)
> [result,fval,exitflag,output] = fminbnd( objfcn,LB,UB)
> M(x,y) = result
> else M(x,y) = 0
> end
> end
>
>

First, I think there's a problem with your existing code. The vector X contains 0.1, 0.2, 0.3 ..., but in the loop x gets successive values 1, 2, 3 ... . Do you actually want x to take on successive values from X? Likewise for Y and y?

Assuming that you want to use the values from X and Y, the code would go something like this (needs checking):

M = zeros(length(X), length(Y); % avoid having to assign zeros inside loop
for xindex = 1:length(X)
   x = X(xindex);
   Yvalid = Y(Y <= x/(1-a));
   for yindex = 1:length(Yvalid)
     y = Yvalid(yindex);
     ... % compute function of x and y
     M(xindex, yindex) = ... whatever
   end
end

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