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:
Avoiding for loop (with a custom function)

Subject: Avoiding for loop (with a custom function)

From: stinkinrich88

Date: 4 Nov, 2009 11:54:02

Message: 1 of 4

Hello,

I have the following code in my program:

for i_node = 1 : n_node
        point_array(i_node, :, :) = mob_model(n_dim, n_step);
end

The function mob_model returns a trajectory (array of positions) of a single node. n_dim says how many dimensions the region has and n_steps says how many timesteps to generate positions for. i.e. mob_model returns an array of size n_dim x n_step.

This is done for n_node nodes using the for loop. But surely there is a way to avoid this for loop? I was hoping for something like:

point_array(1 : n_node, :, :) = mob_model(n_dim, n_step);

But it doesn't work. I think it would be good practice to keep mob_model returning a single trajectory, but maybe I'm wrong.

Any ideas? Thanks.
Rich

Subject: Avoiding for loop (with a custom function)

From: Cristian D

Date: 11 Nov, 2009 12:27:03

Message: 2 of 4

"stinkinrich88" <stinkinrich88@gmail.com> wrote in message <hcrq0q$ast$1@fred.mathworks.com>...
> Hello,
>
> I have the following code in my program:
>
> for i_node = 1 : n_node
> point_array(i_node, :, :) = mob_model(n_dim, n_step);
> end
>
> The function mob_model returns a trajectory (array of positions) of a single node. n_dim says how many dimensions the region has and n_steps says how many timesteps to generate positions for. i.e. mob_model returns an array of size n_dim x n_step.
>
> This is done for n_node nodes using the for loop. But surely there is a way to avoid this for loop? I was hoping for something like:
>
> point_array(1 : n_node, :, :) = mob_model(n_dim, n_step);
>
> But it doesn't work. I think it would be good practice to keep mob_model returning a single trajectory, but maybe I'm wrong.
>
> Any ideas? Thanks.
> Rich



Hello

Try with this:

point_array(1, :, :) = mob_model(n_dim, n_step);
repmat( point_array, [n_node 1 1] );

I hope you find it useful.
Regards

Subject: Avoiding for loop (with a custom function)

From: stinkinrich88

Date: 11 Nov, 2009 12:35:06

Message: 3 of 4

"Cristian D" <cdorbesi@gmail.com> wrote in message <hdeain$667$1@fred.mathworks.com>...
> "stinkinrich88" <stinkinrich88@gmail.com> wrote in message <hcrq0q$ast$1@fred.mathworks.com>...
> > Hello,
> >
> > I have the following code in my program:
> >
> > for i_node = 1 : n_node
> > point_array(i_node, :, :) = mob_model(n_dim, n_step);
> > end
> >
> > The function mob_model returns a trajectory (array of positions) of a single node. n_dim says how many dimensions the region has and n_steps says how many timesteps to generate positions for. i.e. mob_model returns an array of size n_dim x n_step.
> >
> > This is done for n_node nodes using the for loop. But surely there is a way to avoid this for loop? I was hoping for something like:
> >
> > point_array(1 : n_node, :, :) = mob_model(n_dim, n_step);
> >
> > But it doesn't work. I think it would be good practice to keep mob_model returning a single trajectory, but maybe I'm wrong.
> >
> > Any ideas? Thanks.
> > Rich
>
>
>
> Hello
>
> Try with this:
>
> point_array(1, :, :) = mob_model(n_dim, n_step);
> repmat( point_array, [n_node 1 1] );
>
> I hope you find it useful.
> Regards

Hello, I forgot to say that mob_model has a random element (it creates random mobility trajectories) so I don't want duplicates. Pretty sure your code will duplicate just one trajectory (correct me if I'm wrong!) but thank you for helping!

Subject: Avoiding for loop (with a custom function)

From: Jos

Date: 11 Nov, 2009 13:28:03

Message: 4 of 4

"stinkinrich88" <stinkinrich88@gmail.com> wrote in message <hcrq0q$ast$1@fred.mathworks.com>...
> Hello,
>
> I have the following code in my program:
>
> for i_node = 1 : n_node
> point_array(i_node, :, :) = mob_model(n_dim, n_step);
> end
>
> The function mob_model returns a trajectory (array of positions) of a single node. n_dim says how many dimensions the region has and n_steps says how many timesteps to generate positions for. i.e. mob_model returns an array of size n_dim x n_step.
>
> This is done for n_node nodes using the for loop. But surely there is a way to avoid this for loop? I was hoping for something like:
>
> point_array(1 : n_node, :, :) = mob_model(n_dim, n_step);
>
> But it doesn't work. I think it would be good practice to keep mob_model returning a single trajectory, but maybe I'm wrong.
>
> Any ideas? Thanks.
> Rich

If I understood you post correctly, mob_model(C1,C2) will return a semi-random matrix of a particular size. That is, each time it is called it will give a different result.
If that is the case, there is no easy way to vectorize it other than:
1) modify the code of "mob_model" to make it vectorized
2) use ARRAYFUN

fh = @(x) rand(2,3) % note that x is not used, like "i_node" above
C = arrayfun(fh,1:3,'un',0)
C = cat(3,C{:}) % concatenate into one array

hth
Jos

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