Vectorize loop with function containing randoms

Hello,
I would like to know if there is any way to vectorize this:
ds=zeros(1,max);
for i=1:max
ds(i)=containsRandoms(p1);
end
p1 is always a fixed scalar, I just want to execute the function many times to fill ds. The function I'm calling inside the loop contains randoms and many other calculations, so it will produce a different result each time.
I was trying something like
ds(1:max)=containsRandoms(p1);
but it is only executing containsRandoms once, and therefore ds is filled with the same value repeated max times.
Thanks in advance.

回答 (3 件)

Peter
Peter 2011 年 10 月 14 日

1 投票

You'd have to vectorize containsRandoms, not just the assignment to ds.

4 件のコメント

sforza
sforza 2011 年 10 月 14 日
containsRandoms is already vectorized. I just went through it and my other 5 functions it calls, and realized this was the last for loop I had remaining, I mean, in my whole code there is no for loops at all anymore, that was the last one I was unable to get rid of.
Could this explain the lack of speed improvement?
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 14 日
The for-loop in new version of MATLAB has been very efficient. Sometimes it's even faster then no-loop approach.
Sean de Wolski
Sean de Wolski 2011 年 10 月 14 日
Yes. Avoid arrayfun. A for-loop is easier to understand and probably faster, especially in older versions.
sforza
sforza 2011 年 10 月 14 日
I'm using 2010b for no specific reason, I could also switch to 2011b, I'm on a campus license. Is there any noticeable improvement in the for loops speed between these versions?
Thanks for all your anwers!
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 14 日

0 投票

First, don't use max as a variable name as max() is a popular function.
Second, Are you looking at: ds=rand(5,3) for example?
Or:
a=magic(3);
b=arrayfun(@containsRandoms,a)
Assume your function containsRandoms() is something like this:
function out=containsRandoms(in)
out=in+rand;

5 件のコメント

sforza
sforza 2011 年 10 月 14 日
Hi, thanks for replying.
I used max because I quickly simplified the code to post it here, I am actually using another name.
My function "containsRandoms" is quite more complicated than just rand, and calls other functions also, but I would say yes, somehow, that's what I want, turn
for i=1:3
ds(i)=rand(1);
end
into
rand(1,3)
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 14 日
So is the function rand() the solution, or what do you really want to achieve?
sforza
sforza 2011 年 10 月 14 日
No, the function rand() is not the solution.
I'm sorry if maybe I didn't make myself clear, I only meant that my function containsRandoms behaves like rand in the sense that the result is different each time you call it, because it contains rands.
What I want to achieve is filling the m positions of a vector with the results of calling my function "containsRandoms" m times, without using a for loop.
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 14 日
Then, arrayfun() is probably what you are looking for. See update in the answer.
sforza
sforza 2011 年 10 月 14 日
You are right, arrayfun is what I was looking for. Thank you.
Andrei Bobrov
Andrei Bobrov 2011 年 10 月 14 日

0 投票

ds = arrayfun(@(i1)containsRandoms(p1),1:max1);
%if size of 'ds' m x n eg. 2 x 2
ds = reshape(arrayfun(@(i1)containsRandoms(p1),1:2*2),2,[]);

2 件のコメント

sforza
sforza 2011 年 10 月 14 日
You are also right, thanks andrei for the detailed sintax, I'm using this one.
After checking the documentation, I'm not sure what does @(i1) do. Could you please explain it a little bit more in depth?
However, I've done a couple of quick tests and there is no noticeable speed improvement, this new version is even sligthly slower than the loop version from my first post. I've tried with 10000 iterations. Is this normal? I was expecting the opposite behaviour, it to be faster.
At least looks cleaner, that's for sure :)
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 14 日
Search for "anonymous function" in documentation to understand @().

この質問は閉じられています。

質問済み:

2011 年 10 月 13 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by