From: Enrico on
I'm trying to do the following:

t = zeros(500,500);
f = [345,234,64,4356, ... (list of points to change)
for i = f
t[i]=customrandomfunc()
end

this takes a very long time, is there a way to speed this up?

I'd like something like:
t[f] = customrandomfunc()
except I want each point to have a different value.

customrandomfunc picks a "random" number from a dataset and does not depend on t or f.
From: Oleg Komarov on
"Enrico " <en(a)rcsnetwork.com> wrote in message <i106gg$evi$1(a)fred.mathworks.com>...
> I'm trying to do the following:
>
> t = zeros(500,500);
> f = [345,234,64,4356, ... (list of points to change)
> for i = f
> t[i]=customrandomfunc()
> end
>
> this takes a very long time, is there a way to speed this up?
>
> I'd like something like:
> t[f] = customrandomfunc()
> except I want each point to have a different value.
>
> customrandomfunc picks a "random" number from a dataset and does not depend on t or f.

You have already preallocated the output, which means that customrandomfunc is taking most of the time.
Type the following:
profile on
t = zeros(500,500);
f = [345,234,64,4356, ... (list of points to change)
for i = f
t[i]=customrandomfunc()
end

profile off
profile viewer

Tell us what's taking much to execute and we can advice on that.

Oleg
From: Matt Fig on
Why not just modify your ustomrandomfunc to take vector arguments? It seems to me that this is the most likely bottleneck.
From: Enrico on
Yes, now that I think of it, you are both correct.

Here is the profile info.

Function NameCallsTotal TimeSelf Time*Total Time Plot(dark band = self time)
lookup_dat 371782 5.692 s 0.968 s
cell.ismember 371782 4.287 s 3.764 s
ismember 743561 8.789 s 6.216 s
unique 74356 8.668 s 5.794 s
sortrows 74356 5.388 s 4.248 s
iscellstr 74356 1.734 s 1.734 s
repmat 37178 1.390 s 1.390 s
sortrowsc (MEX-function) 74356 1.140 s 1.140 s
linspace 6 0 s 0.000 s
rad2deg 6 0 s 0.000 s

lookup_dat is my randomfunction.
The code is shown below:

if abs(error) >= 90, error = 0; end
obs_az = obs_az + error*sin(2*(rand()-.5));
obs_el = obs_el + error*sin(2*(rand()-.5));
angle = round(obs_az - angle);
obs_el = round(obs_el);
ridx = ceil(size(type,2)*rand());
type_idx=find(ismember(dbhdr, type(ridx))==1);
if angle < 0, angle = angle + 360; end
if angle >= 360, angle = angle - 359; end
if obs_el < 0, obs_el = obs_el + 90; end
if obs_el >= 90, obs_el = obs_el - 89; end
value=dbdat(obs_el+1,angle+1,type_idx);


error: defines the magnitude of "randomness" to put in
type is an array of strings. I pick a random one.
dbhdr contains a list of strings that I need to lookup an index for based on type
dbdat contains the data with the dbhdr index as an input

I need to do this for every point.
I'm guessing that "sortrows" is used by find.
I'm not sure about "isunique" and "ismember"