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:
Loop / indexing query

Subject: Loop / indexing query

From: DRG

Date: 17 Dec, 2011 12:55:04

Message: 1 of 5

Hi all..

I have a code that you input two variables, S and R and it does a
simlation, producing a 800 x 1 array called E. It also loads pre-
existing data and calculates the R2 value.

This is fine and works well. But I know that S lies between two values
(9000 and 10,000) and R lies between two values (7000 and 8500). I
wanted to make a double for loop something like

for S = 9000:1:10000
 for R = 7000:1:8500

but I run into problems here, as

E(S,R) will give an error message, as indeed it should. Each answer
for E is a 1x800 array and obviously calling E(S,R) makes no sense but
I have no idea how to call it. Does anyone know how I should, so for
each value of S and R I can get a 1 x 800 value ?

Please advise asap!

Dave

Subject: Loop / indexing query

From: dpb

Date: 17 Dec, 2011 14:31:41

Message: 2 of 5

On 12/17/2011 6:55 AM, DRG wrote:
> Hi all..
>
> I have a code that you input two variables, S and R and it does a
> simlation, producing a 800 x 1 array called E. It also loads pre-
> existing data and calculates the R2 value.
>
> This is fine and works well. But I know that S lies between two values
> (9000 and 10,000) and R lies between two values (7000 and 8500). I
> wanted to make a double for loop something like
>
> for S = 9000:1:10000
> for R = 7000:1:8500
>
> but I run into problems here, as
>
> E(S,R) will give an error message, as indeed it should. Each answer
> for E is a 1x800 array and obviously calling E(S,R) makes no sense but
> I have no idea how to call it. Does anyone know how I should, so for
> each value of S and R I can get a 1 x 800 value ?
...

Either run the loops over 1:N, 1:M and create the value inside the loops
instead of using the indices or vice versa; use the value as loops but
create the indices. I'd do the former, probably...

Preallocate E, of course for speed...

E=zeros(1001,1501);
S=9000;
for s=1:N
   R=7000;
   for r=1:M
     E(s,r)=yourfunction(S,R);
     %anything else inside r loop here
     R=R+1;
   end
   %anything else inside s loop here
   S=S+1;
end

--

Subject: Loop / indexing query

From: DRG

Date: 17 Dec, 2011 16:13:03

Message: 3 of 5

Thanks for the reply, but I'm not sure what you mean ? Let's say the
code looks like this;

for s = 9000:1:10000

    for r = 7000:1:8500


Function() %calls a seperature M file that uses the value for s and
r to generate a 1 x 800 matrix E


Answer(s,r) = ?? %
end
end

Please do guide me on this, sorry Im so clueless!






On Dec 17, 2:31 pm, dpb <n...@non.net> wrote:
> On 12/17/2011 6:55 AM, DRG wrote:
>
>
>
>
>
>
>
> > Hi all..
>
> > I have a code that you input two variables, S and R and it does a
> > simlation, producing a 800 x 1 array called E. It also loads pre-
> > existing data and calculates the R2 value.
>
> > This is fine and works well. But I know that S lies between two values
> > (9000 and 10,000) and R lies between two values (7000 and 8500).  I
> > wanted to make a double for loop something like
>
> > for S = 9000:1:10000
> >   for R = 7000:1:8500
>
> > but I run into problems here, as
>
> > E(S,R)  will give an error message, as indeed it should. Each answer
> > for E is a 1x800 array and obviously calling E(S,R) makes no sense but
> > I have no idea how to call it. Does anyone know how I should, so for
> > each value of S and R I can get a 1 x 800 value ?
>
> ...
>
> Either run the loops over 1:N, 1:M and create the value inside the loops
> instead of using the indices or vice versa; use the value as loops but
> create the indices.  I'd do the former, probably...
>
> Preallocate E, of course for speed...
>
> E=zeros(1001,1501);
> S=9000;
> for s=1:N
>    R=7000;
>    for r=1:M
>      E(s,r)=yourfunction(S,R);
>      %anything else inside r loop here
>      R=R+1;
>    end
>    %anything else inside s loop here
>    S=S+1;
> end
>
> --

Subject: Loop / indexing query

From: dpb

Date: 17 Dec, 2011 17:33:44

Message: 4 of 5

On 12/17/2011 10:13 AM, DRG wrote:
> On Dec 17, 2:31 pm, dpb<n...@non.net> wrote:

...[top posting repaired. Please don't; hard conversation follow makes]...

...

>>> for S = 9000:1:10000
>>> for R = 7000:1:8500
>>
>>> but I run into problems here, as
>>
>>> E(S,R) will give an error message, as indeed it should. Each answer
>>> for E is a 1x800 array and obviously calling E(S,R) makes no sense but
>>> I have no idea how to call it. Does anyone know how I should, so for
>>> each value of S and R I can get a 1 x 800 value ?
>>
>> ...
>>
>> Either run the loops over 1:N, 1:M and create the value inside the loops
>> instead of using the indices or vice versa; use the value as loops but
>> create the indices. I'd do the former, probably...
>>
>> Preallocate E, of course for speed...
>>
>> E=zeros(1001,1501);
>> S=9000;
>> for s=1:N
>> R=7000;
>> for r=1:M
>> E(s,r)=yourfunction(S,R);
>> %anything else inside r loop here
>> R=R+1;
>> end
>> %anything else inside s loop here
>> S=S+1;
>> end
...
 > Thanks for the reply, but I'm not sure what you mean ? Let's say the
 > code looks like this;

I mean exactly what I wrote... :)

 > for s = 9000:1:10000
 > for r = 7000:1:8500
...

 > Please do guide me on this...

Did above. Don't run the loop over s,r; use 1:length(9000:10000) and
1:length(7000:8500) instead for the loops themselves and compute the
argument values for the function separately.

Run the following sample code and generalize to your situation of exact
numbers...

E=zeros(3,5);
S=9000;
for s=1:3
   R=7000;
   for r=1:5
     E{s,r}=func(S,R);
     R=R+1;
   end
   S=S+1;
end

function ret=func(x,y)
   ret=rand(5,1)*(x/y);

This returns a cell array E each cell of which for each combination of S
and R column vector of length 5 (the results of the function func).

Plug in your values and function and voila!

If that's not the result you're shooting for you need a better/different
problem description.

Read on cell arrays in the help documentation.

--

Subject: Loop / indexing query

From: dpb

Date: 17 Dec, 2011 20:17:44

Message: 5 of 5

On 12/17/2011 6:55 AM, DRG wrote:
...

> I have a code that you input two variables, S and R and it does a
> simlation, producing a 800 x 1 array called E. It also loads pre-
> existing data and calculates the R2 value.
>
> This is fine and works well. But I know that S lies between two values
> (9000 and 10,000) and R lies between two values (7000 and 8500). I
> wanted to make a double for loop something like
>
> for S = 9000:1:10000
> for R = 7000:1:8500
>
> but I run into problems here, as
>
> E(S,R) will give an error message, as indeed it should. Each answer
> for E is a 1x800 array and obviously calling E(S,R) makes no sense but
> I have no idea how to call it....

While the other posting answers the question asked, I'll point out here
that "the Matlab way" would be to vectorize the function to accept
vector arguments (or the limits and internalize the vectorization) and
return the desired result to the calling routine directly.

You might want to peruse the info at

doc meshgrid

for some other useful pieces of the puzzle...

--

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