Got Questions? Get Answers.
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 random iterations

Subject: for loop random iterations

From: Erik L

Date: 2 Jun, 2010 19:42:20

Message: 1 of 5

This is my simple for loop:

for j=1:5,
 for i=1:10,

   whatever operation

 end
end

i want to make it randomize, like the FOR LOOP for i iterates by picking from 1 to 10(each and once) randomly for each normal iteration of j.

for example for j=1, i need i to be iterated like:

i=[2 4 5 3 1 .....] randomly and each one once in a set [1 to 10].

and similarly for each iteration of j, the loop for i should iterate randomly.


Is there a way to solve it in Matlab.

Many thanks

Erik. L

Subject: for loop random iterations

From: Sadik

Date: 2 Jun, 2010 20:17:21

Message: 2 of 5

Hi Erik,

One solution:

for j = 1:5
    for i = randperm(10)
           ....
    end
end

randperm(n) returns a random permutations of numbers from 1 to n. You also know that after defining a vector like

v = [2 3 1 4 5 3]; % for example

it is perfectly legitimate to write it into your for loop as follows:

for counter = v
...
end

so that the counter is going to take the values in v. That is, 2, 3, 1 ...

Best.


"Erik L" <lookgrn@gmail.com> wrote in message <hu6c6s$mpo$1@fred.mathworks.com>...
> This is my simple for loop:
>
> for j=1:5,
> for i=1:10,
>
> whatever operation
>
> end
> end
>
> i want to make it randomize, like the FOR LOOP for i iterates by picking from 1 to 10(each and once) randomly for each normal iteration of j.
>
> for example for j=1, i need i to be iterated like:
>
> i=[2 4 5 3 1 .....] randomly and each one once in a set [1 to 10].
>
> and similarly for each iteration of j, the loop for i should iterate randomly.
>
>
> Is there a way to solve it in Matlab.
>
> Many thanks
>
> Erik. L

Subject: for loop random iterations

From: TideMan

Date: 2 Jun, 2010 20:19:45

Message: 3 of 5

On Jun 3, 7:42 am, "Erik L" <look...@gmail.com> wrote:
> This is my simple for loop:
>
> for j=1:5,
>  for i=1:10,
>
>    whatever operation
>
>  end
> end
>
> i want to make it randomize, like the FOR LOOP for i iterates by picking from 1 to 10(each and once) randomly for each normal iteration of j.
>
> for example for j=1, i need i to be iterated like:
>
> i=[2 4 5 3 1 .....] randomly and each one once in a set [1 to 10].
>
> and similarly for each iteration of j, the loop for i should iterate randomly.
>
> Is there a way to solve it in Matlab.
>
> Many thanks
>
> Erik. L

indx=randperm(10);
for ix=1:10
iy=indx(ix); % Now use iy as your index
-
-
end

Note: don't use i or j for indexes. By default, they are sqrt(-1).

Subject: for loop random iterations

From: Roger Stafford

Date: 2 Jun, 2010 20:29:21

Message: 4 of 5

"Erik L" <lookgrn@gmail.com> wrote in message <hu6c6s$mpo$1@fred.mathworks.com>...
> This is my simple for loop:
>
> for j=1:5,
> for i=1:10,
>
> whatever operation
>
> end
> end
>
> i want to make it randomize, like the FOR LOOP for i iterates by picking from 1 to 10(each and once) randomly for each normal iteration of j.
>
> for example for j=1, i need i to be iterated like:
>
> i=[2 4 5 3 1 .....] randomly and each one once in a set [1 to 10].
>
> and similarly for each iteration of j, the loop for i should iterate randomly.
>
>
> Is there a way to solve it in Matlab.
>
> Many thanks
>
> Erik. L

[J,I] = meshgrid(1:5,1:10);
p = randperm(5*10);
I = I(p); J = J(p);
for k = 1:5*10
 i = I(k); j = J(k);
 Do your thing with i and j
end

That should truly randomize the sequence order of i, j pairs.

Roger Stafford

Subject: for loop random iterations

From: Erik L

Date: 2 Jun, 2010 20:55:21

Message: 5 of 5

Thanks all, it solved my problem :>))

Cheers
Erik L

"Erik L" <lookgrn@gmail.com> wrote in message <hu6c6s$mpo$1@fred.mathworks.com>...
> This is my simple for loop:
>
> for j=1:5,
> for i=1:10,
>
> whatever operation
>
> end
> end
>
> i want to make it randomize, like the FOR LOOP for i iterates by picking from 1 to 10(each and once) randomly for each normal iteration of j.
>
> for example for j=1, i need i to be iterated like:
>
> i=[2 4 5 3 1 .....] randomly and each one once in a set [1 to 10].
>
> and similarly for each iteration of j, the loop for i should iterate randomly.
>
>
> Is there a way to solve it in Matlab.
>
> Many thanks
>
> Erik. L

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