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:
help in loop, very important for me friends

Subject: help in loop, very important for me friends

From: mahdi roozbhani

Date: 22 Dec, 2010 04:15:06

Message: 1 of 11

friends, I have a code in below :

x=[1 2 2 3 4];
sx=size(x);

for i=1:3
randx=randi(sx(2),1,1);
e=x(randx);
if e==2
p(i)=e;
else
i=i-1;
end
end

for example in the i equal to 3,if e is equal 2; do the third(3th) loop again.

but it does not work, please help me.
for example, I want to repeat counter i in 3rd level for several time.

Subject: help in loop, very important for me friends

From: Rune Allnor

Date: 22 Dec, 2010 04:22:49

Message: 2 of 11

On Dec 22, 5:15 am, "mahdi roozbhani" <m.m.roozbah...@gmail.com>
wrote:

> for i=1:3
> i=i-1;
> end

Rune

Subject: help in loop, very important for me friends

From: mahdi roozbhani

Date: 22 Dec, 2010 04:28:05

Message: 3 of 11

Rune Allnor <allnor@tele.ntnu.no> wrote in message <68e28488-6337-493d-b86a-858f7dc8cd71@32g2000yqz.googlegroups.com>...
> On Dec 22, 5:15 am, "mahdi roozbhani" <m.m.roozbah...@gmail.com>
> wrote:
>
> > for i=1:3
> > i=i-1;
> > end
>
> Rune

.......................................................................

This way is not working, u could see in my code that I did it this approach, but it did not work.

Subject: help in loop, very important for me friends

From: ImageAnalyst

Date: 22 Dec, 2010 04:55:35

Message: 4 of 11

This seems so easy, if you described it right. Why don't you just do
this:

x=[1 2 2 3 4];
sx=size(x);

for i=1:3
fprintf(1, 'Now running iteration with i = %d\n', i);
randx=randi(sx(2),1,1)
e=x(randx)
if e==2
p(i)=e;
else
i=i-1;
end
         % Repeat if i = 3.
if i == 3
fprintf(1, 'Now running second time through for i = 3\n');
randx=randi(sx(2),1,1)
e=x(randx)
if e==2
p(i)=e;
else
i=i-1;
end
end
end

Now running iteration with i = 1
randx =
     2
e =
     2
Now running iteration with i = 2
randx =
     5
e =
     4
Now running iteration with i = 3
randx =
     2
e =
     2
Now running second time through for i = 3
randx =
     1
e =
     1

Subject: help in loop, very important for me friends

From: Walter Roberson

Date: 22 Dec, 2010 05:00:59

Message: 5 of 11

On 21/12/10 10:15 PM, mahdi roozbhani wrote:
> friends, I have a code in below :
>
> x=[1 2 2 3 4];
> sx=size(x);
>
> for i=1:3
> randx=randi(sx(2),1,1);
> e=x(randx);
> if e==2
> p(i)=e;
> else
> i=i-1;
> end
> end
>
> for example in the i equal to 3,if e is equal 2; do the third(3th) loop
> again.
>
> but it does not work, please help me.
> for example, I want to repeat counter i in 3rd level for several time.

You cannot do that with a "for" loop. It is allowed to change the value
of the "for" counter variable within a cycle of the loop, but when the
next cycle starts, the changed value will be ignored and the next value
will be proceeded to just as if the counter variable had not been changed.

for i = 1:3
   repeat_it = true;
   while repeat_it
     randx=randi(sx(2),1,1);
     e = x(randx);
     if e==2
       p(i) = e;
       repeat_it = false;
     else
       repeat_it = true;
     end
   end
end

Subject: help in loop, very important for me friends

From: mahdi roozbhani

Date: 22 Dec, 2010 05:48:04

Message: 6 of 11

my friends please forgive me, because I mean that e~=2 not e==2 and I want that the output of this code becomes p=[1 3 4]

 x=[1 2 2 3 4];
> > sx=size(x);
> >
> > for i=1:3
> > randx=randi(sx(2),1,1);
> > e=x(randx);
> > if e==2
> > p(i)=e;
> > else
> > i=i-1;
> > end
> > end

Subject: help in loop, very important for me friends

From: mahdi roozbhani

Date: 22 Dec, 2010 05:50:21

Message: 7 of 11

Walter Roberson <roberson@hushmail.com> wrote in message <gCfQo.3857$ge2.975@newsfe07.iad>...
> On 21/12/10 10:15 PM, mahdi roozbhani wrote:
> > friends, I have a code in below :
> >
> > x=[1 2 2 3 4];
> > sx=size(x);
> >
> > for i=1:3
> > randx=randi(sx(2),1,1);
> > e=x(randx);
> > if e==2
> > p(i)=e;
> > else
> > i=i-1;
> > end
> > end
> >
> > for example in the i equal to 3,if e is equal 2; do the third(3th) loop
> > again.
> >
> > but it does not work, please help me.
> > for example, I want to repeat counter i in 3rd level for several time.
>
> You cannot do that with a "for" loop. It is allowed to change the value
> of the "for" counter variable within a cycle of the loop, but when the
> next cycle starts, the changed value will be ignored and the next value
> will be proceeded to just as if the counter variable had not been changed.
>
> for i = 1:3
> repeat_it = true;
> while repeat_it
> randx=randi(sx(2),1,1);
> e = x(randx);
> if e==2
> p(i) = e;
> repeat_it = false;
> else
> repeat_it = true;
> end
> end
> end



''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

my friends please forgive me, because I mean that e~=2 not e==2 and I want that the output of this code becomes p=[1 3 4]

 x=[1 2 2 3 4];
> > sx=size(x);
> >
> > for i=1:3
> > randx=randi(sx(2),1,1);
> > e=x(randx);
> > if e==2
> > p(i)=e;
> > else
> > i=i-1;
> > end
> > end

Subject: help in loop, very important for me friends

From: Walter Roberson

Date: 22 Dec, 2010 05:53:44

Message: 8 of 11

On 21/12/10 11:50 PM, mahdi roozbhani wrote:

> my friends please forgive me, because I mean that e~=2 not e==2 and I
> want that the output of this code becomes p=[1 3 4]

Then why are you selecting the values of x at random? When you select at
random, all you can be sure of is that at the end the values stored in p
will be drawn "with replacement" from the set {1, 3, 4}.

Subject: help in loop, very important for me friends

From: mahdi roozbhani

Date: 22 Dec, 2010 07:08:04

Message: 9 of 11

Walter Roberson <roberson@hushmail.com> wrote in message <IngQo.16327$My1.3299@newsfe16.iad>...
> On 21/12/10 11:50 PM, mahdi roozbhani wrote:
>
> > my friends please forgive me, because I mean that e~=2 not e==2 and I
> > want that the output of this code becomes p=[1 3 4]
>
> Then why are you selecting the values of x at random? When you select at
> random, all you can be sure of is that at the end the values stored in p
> will be drawn "with replacement" from the set {1, 3, 4}.


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
because I wrote a code that random is interfered in it and for completing of my code ,I need this approach, but I dont know how.

Subject: help in loop, very important for me friends

From: Walter Roberson

Date: 22 Dec, 2010 07:38:24

Message: 10 of 11

On 22/12/10 1:08 AM, mahdi roozbhani wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <IngQo.16327$My1.3299@newsfe16.iad>...
>> On 21/12/10 11:50 PM, mahdi roozbhani wrote:
>>
>> > my friends please forgive me, because I mean that e~=2 not e==2 and I
>> > want that the output of this code becomes p=[1 3 4]
>>
>> Then why are you selecting the values of x at random? When you select
>> at random, all you can be sure of is that at the end the values stored
>> in p will be drawn "with replacement" from the set {1, 3, 4}.
>
>
> '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>
> because I wrote a code that random is interfered in it and for
> completing of my code ,I need this approach, but I dont know how.

If you need to do random selection, then you are unlikely to get p=[1 3
4] as your result. The code I showed before should be okay for the
random selection "with replacement" case, if you just change "if e==2"
to "if e~=2".

If you need to get p=[1 3 4] as your output, then I do not understand
where the random selection should fit in.


What I also do not understand is why you do not do something like:

newx = x(x~=2);
numnewx = length(numnewx);
for K = 1:numnewx
   e = newx(randi(numnewx,1,1));
   p(K) = e;
end

That is, if one of the values in the array of possibilities is
prohibited, then filter it out of the array you are selecting from
rather than bothering with looping around until you have selected some
other value.

Subject: help in loop, very important for me friends

From: BartonJanis30

Date: 1 Jan, 2011 07:54:17

Message: 11 of 11

Some time ago, I did need to buy a good car for my organization but I did not earn enough money and couldn't purchase anything. Thank heaven my brother proposed to try to get the <a href="http://bestfinance-blog.com/topics/credit-loans">credit loans</a> from creditors. Therefore, I acted so and was happy with my secured loan.

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