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:
generate random vectors of various lengths

Subject: generate random vectors of various lengths

From: Oriole

Date: 23 Sep, 2009 21:05:20

Message: 1 of 9

Say I have a vector a=[12 34 11 5 9 23]

I would like to generate

for j=1:length(a)
rand(1, a(j))
end

but I don't want to use this "for" loop, since my "a" would be as large as 1*100000, and actually the components of "a" can be large numbers like 9000.

How to generate these random vector rand(1, a(j)) at one time, and how to store all these random vectors (of various length) in one object for futher calculations, shall I use a "cell"?

Thanks
Oriole

Subject: generate random vectors of various lengths

From: dpb

Date: 23 Sep, 2009 22:27:21

Message: 2 of 9

Oriole wrote:
> Say I have a vector a=[12 34 11 5 9 23]
>
> I would like to generate
>
> for j=1:length(a) rand(1, a(j)) end
>
> but I don't want to use this "for" loop, since my "a" would be as
> large as 1*100000, and actually the components of "a" can be large
> numbers like 9000.
>
> How to generate these random vector rand(1, a(j)) at one time, and
> how to store all these random vectors (of various length) in one
> object for futher calculations, shall I use a "cell"?

Cell array is only choice for a ragged array in ML afaik...

You may well run into other issues (like memory) if try to generate and
hold 10E5*10E4 =~> 10E9 rv's anyway so the for loop may be the least of
your worries...

--

Subject: generate random vectors of various lengths

From: Jos

Date: 24 Sep, 2009 05:58:01

Message: 3 of 9

dpb <none@non.net> wrote in message <h9e7l0$43d$1@news.eternal-september.org>...
> Oriole wrote:
> > Say I have a vector a=[12 34 11 5 9 23]
> >
> > I would like to generate
> >
> > for j=1:length(a) rand(1, a(j)) end
> >
> > but I don't want to use this "for" loop, since my "a" would be as
> > large as 1*100000, and actually the components of "a" can be large
> > numbers like 9000.
> >
> > How to generate these random vector rand(1, a(j)) at one time, and
> > how to store all these random vectors (of various length) in one
> > object for futher calculations, shall I use a "cell"?

You can; here is one way to do so:

a=[3 6 2 8] ;
C = arrayfun(@(x) rand(1,x), a, 'un',false) % cell array

> Cell array is only choice for a ragged array in ML afaik...

Not really true. Other options are structure arrays, or an associated group vector.

> You may well run into other issues (like memory) if try to generate and
> hold 10E5*10E4 =~> 10E9 rv's anyway so the for loop may be the least of
> your worries...

and this is still true of course ...

Jos

Subject: generate random vectors of various lengths

From: Oriole

Date: 24 Sep, 2009 11:42:02

Message: 4 of 9

Thank you Jos and Dpb.

I tried arrayfun() yesterday actually, and vectorized my code. But I did not see any speed gain. It actually get slightly slower than the previous for loop. Anyone knows why it is like that.

And also a general question: I am trying to use Matlab to simulate rare events, that is why my number of trials must be very large, like 50 million or preferably 100 million sth like that. It takes more than one hour with 50 milions trials, I have not dared to try 100 million trials, but I would really like to. What is the best way to do such heavy simulation. Is matlab a good choice or should I use C++ (have never used C though) instead or any other tips?

Subject: generate random vectors of various lengths

From: Peter Perkins

Date: 24 Sep, 2009 12:03:22

Message: 5 of 9

Oriole wrote:
> Say I have a vector a=[12 34 11 5 9 23]
>
> I would like to generate
>
> for j=1:length(a)
> rand(1, a(j))
> end

This may or may not be faster than anything you've tried, and certainly is no different w.r.t. memory:

>> a = [12 34 11 5 9 23]
a =
    12 34 11 5 9 23
>> mat2cell(rand(1,sum(a)),1,a)
ans =
    [1x12 double] [1x34 double] [1x11 double] [1x5 double] [1x9 double] [1x23 double]
Hope this helps.

Subject: generate random vectors of various lengths

From: Tom Lane

Date: 24 Sep, 2009 14:42:47

Message: 6 of 9

>>> a = [12 34 11 5 9 23]
> a =
> 12 34 11 5 9 23
>>> mat2cell(rand(1,sum(a)),1,a)
> ans = [1x12 double] [1x34 double] [1x11 double] [1x5 double]
> [1x9 double] [1x23 double]

Same idea, if you are willing to store the values in a single vector and
maintain indices into that vector:

>> a=[12 34 11 5 9 23]
a =
    12 34 11 5 9 23
>> start = 1+cumsum([0,a(1:end-1)])
start =
     1 13 47 58 63 72
>> stop = cumsum(a)
stop =
    12 46 57 62 71 94
>> r = rand(1,sum(a));
>> sample3 = r(start(3):stop(3))

-- Tom

Subject: generate random vectors of various lengths

From: Jos

Date: 24 Sep, 2009 15:09:03

Message: 7 of 9

"Oriole " <oriole_ni@hotmail.com> wrote in message <h9flua$5h2$1@fred.mathworks.com>...
> Thank you Jos and Dpb.
>
> I tried arrayfun() yesterday actually, and vectorized my code. But I did not see any speed gain. It actually get slightly slower than the previous for loop. Anyone knows why it is like that.
>
> And also a general question: I am trying to use Matlab to simulate rare events, that is why my number of trials must be very large, like 50 million or preferably 100 million sth like that. It takes more than one hour with 50 milions trials, I have not dared to try 100 million trials, but I would really like to. What is the best way to do such heavy simulation. Is matlab a good choice or should I use C++ (have never used C though) instead or any other tips?

I think learning C(++) takes a lot more time than let the simulation run a few hours more ...
Without knowing your code it is hard to suggest improvements but did you pre-allocate for-loops and/or vectorized efficiently? 10e8 is not that much ...

Compare these

N = 1e4 ;

% 1) not optimized in any way
clear p1 % just to be sure!
tic ;
for k=1:N, p1(k) = 2*k ; end
toc ;

% pre-allocated output
tic ;
p2 = zeros(1,N) ; % pre-allocation
for k=1:N, p2(k) = 2*k ; end
toc ;

% vectorized
tic ;
k = 1:N ; p3 = 2 * k ;
toc ;

isequal(p1,p2,p3)


hth
Jos

Subject: generate random vectors of various lengths

From: dpb

Date: 24 Sep, 2009 16:38:35

Message: 8 of 9

Oriole wrote:
> Thank you Jos and Dpb.
>
> I tried arrayfun() yesterday actually, and vectorized my code. But I
> did not see any speed gain. It actually get slightly slower than the
> previous for loop. Anyone knows why it is like that.

Just way it is oftentimes...the overhead within the arrayfun as compared
to the "straightahead" for loop w/ jit compiling has been discussion of
quite a number of cases recently and fairly frequently the for...end
loop is the performance winner.

...
> do such heavy simulation. Is matlab a good choice or should I use
> C++ (have never used C though) instead or any other tips?

Depends in part on whether this is a one-time (or relatively few) event
or there's going to be a continuing need--actually, I'd say that has a
major bearing on the decision, probably.

As for ML itself, if there's a core portion that is repetitive it's
certainly true that a mex file can make major performance gains. As for
what language to write it in, my personal suggestion if one isn't
already proficient in C would be Fortran (F95 or later) as the syntax is
much more similar to ML. One handy thing that helps w/o having to deal
with it is that array storage order is consistent between the two
(column-major) whereas C reverses the two so that one has to consciously
think about making sure to access memory as near sequentially as
possible given the problem structure when working thru arrays. Not
doing so can have drastic performance penalties associated with it.

--

Subject: generate random vectors of various lengths

From: dpb

Date: 24 Sep, 2009 16:40:25

Message: 9 of 9

Oriole wrote:
...
> And also a general question: I am trying to use Matlab to simulate
> rare events, that is why my number of trials must be very large, like
> 50 million or preferably 100 million sth like that. It takes more
> than one hour with 50 milions trials, I have not dared to try 100
> million trials, but I would really like to. ...

One other thought -- one might want to look at stratified sampling
techniques...

--

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