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 - variable increment

Subject: for loop - variable increment

From: Mario

Date: 11 Jun, 2008 17:18:02

Message: 1 of 6

Hi.
I want to test an algorithm with different values for the input variables.

I thought to call my function inside a for loop but i want to increment the
input variable with a inconstant increment.

e.g.
for i=0.0001:i:1
   test(i)
end

so the first call would be test(0.0001), the second one test(0.0002), the third
one test(0.0004), then test(0.0008) and so on.

I tried also the following way
i=0.0001
for j=0.0001:i:1
   test(j)
   i=j; % or i=2*i;
end

but it doesn't work!
Any help?

Thank you,

Mario

Subject: for loop - variable increment

From: Mario

Date: 11 Jun, 2008 17:31:03

Message: 2 of 6

found the way

i=0.0001
A=0.0001:(i*2):1
for j=A
   test(j)
end


probably there is a best solution but this works for me..
Anyway I look forward to hearing your hints

"Mario " <nospam@yahoo.com> wrote in message
<g2p1ca$8sa$1@fred.mathworks.com>...
> Hi.
> I want to test an algorithm with different values for the input variables.
>
> I thought to call my function inside a for loop but i want to increment the
> input variable with a inconstant increment.
>
> e.g.
> for i=0.0001:i:1
> test(i)
> end
>
> so the first call would be test(0.0001), the second one test(0.0002), the
third
> one test(0.0004), then test(0.0008) and so on.
>
> I tried also the following way
> i=0.0001
> for j=0.0001:i:1
> test(j)
> i=j; % or i=2*i;
> end
>
> but it doesn't work!
> Any help?
>
> Thank you,
>
> Mario

Subject: for loop - variable increment

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 11 Jun, 2008 17:31:26

Message: 3 of 6

In article <g2p1ca$8sa$1@fred.mathworks.com>, Mario <nospam@yahoo.com> wrote:
>I want to test an algorithm with different values for the input variables.

>I thought to call my function inside a for loop but i want to increment the
>input variable with a inconstant increment.

>e.g.
>for i=0.0001:i:1
> test(i)
>end

You can't do that with a 'for' loop.

>so the first call would be test(0.0001), the second one test(0.0002), the third
>one test(0.0004), then test(0.0008) and so on.

>I tried also the following way
>i=0.0001
>for j=0.0001:i:1
> test(j)
> i=j; % or i=2*i;
>end

>but it doesn't work!

The 0.0001:i:1 part will be logically evaluated first, as if it
produced a vector of values, with j then being sequenced through that
vector of values.

Use a while loop instead.

i = 0.0001;
while i <= 1
  test(i);
  i = i * 2;
end
 
--
  "There is nothing so bad but it can masquerade as moral."
                                              -- Walter Lippmann

Subject: for loop - variable increment

From: Sean

Date: 11 Jun, 2008 17:35:03

Message: 4 of 6

"Mario " <nospam@yahoo.com> wrote in message <g2p1ca$8sa
$1@fred.mathworks.com>...
> Hi.
> I want to test an algorithm with different values for the input variables.
>
> I thought to call my function inside a for loop but i want to increment the
> input variable with a inconstant increment.
>
> e.g.
> for i=0.0001:i:1
> test(i)
> end
>
> so the first call would be test(0.0001), the second one test(0.0002), the
third
> one test(0.0004), then test(0.0008) and so on.
>
> I tried also the following way
> i=0.0001
> for j=0.0001:i:1
> test(j)
> i=j; % or i=2*i;
> end

i has to be an integer.
Try:
for i =1:1000
     test(i*.0001)
end

Subject: for loop - variable increment

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 11 Jun, 2008 17:55:55

Message: 5 of 6

In article <g2p2c7$mia$1@fred.mathworks.com>,
Sean <sean.dewolski@Idontwantspam.umit.maine.edu> wrote:
>"Mario " <nospam@yahoo.com> wrote in message <g2p1ca$8sa
>$1@fred.mathworks.com>...

>> for i=0.0001:i:1
>> test(i)
>> end

>> i=0.0001
>> for j=0.0001:i:1
>> test(j)
>> i=j; % or i=2*i;
>> end

>i has to be an integer.

Where did you get that idea??

doc for

  for x=initval:stepval:endval, statements, end is the same as the
  above syntax, except that loop counter x is incremented (or
  decremented when stepval is negative) by the value stepval on each
  iteration through the loop. The value stepval must be a real number
  or can also be a call to a function that returns a real number.
--
  "Do not on any account attempt to write on both sides of
  the paper at once." -- Walter C. Sellar

Subject: for loop - variable increment

From: us

Date: 11 Jun, 2008 18:00:19

Message: 6 of 6

"Mario ":
<SNIP out of the loop...

> for i=0.0001:i:1
> test(i)
> end
> so the first call would be test(0.0001), the second one
test(0.0002), the third one test(0.0004), then test(0.0008)
and so on...

one of the many solutions

     inc=.0001;
     v=inc*2.^(0:-log2(inc));
for i=v
     disp(i);
end
%{
     0.0001
     0.0002
     0.0004
     0.0008
     0.0016
     0.0032
     0.0064
     0.0128
     0.0256
     0.0512
     0.1024
     0.2048
     0.4096
     0.8192
%}

us

Tags for 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