Optimize this loop to take less computing time

2 次查看(过去 30 天)
Below is a snippet of code which loops through four arrays (stored as structures) and returns an array based on comparison of variables between each of the input arrays while displaying the growing length of dist_in. The output is exactly what I want: an array of se_dist.dist ordered by specific matching indexes in the other arrays. The problem is that this code takes forever to run since se_dist.dist is ~[11000 1] and ptime_in.evid is ~[12400 1] making the total loops in the millions. I am looking for a way to have this maintain the same output while decreasing the computational time.
I have attempted using find, ismember, intersect and others, however intersect seems to only return the first matching index, and the others require arrays of the same length.
h = length(se_dist.dist);
r = length(ptime_in.evid);
dist_in = zeros(r,1);
for k = 1:h
for n = 1:r
if strcmp(se_dist.id(k),ptime_in.sta(n))==1 && se_dist.evid(k)==ptime_in.evid(n)
dist_in(n) = se_dist.dist(k);
end
end
end
A bit of extra information. se_dist.dist is [10880x1 double]. ptime_in.evid is [12413x1 double]. se_dist.id is {10880x1 cell}. ptime_in.sta is {12413x1 cell}. se_dist.evid is [10880x1 double].
I don't mind the code taking a while, however this takes ~20min to run, and this is just a test case. In the future I will be running this where the length of the arrays are on the scale of ~100000 instead of ~10000 which assuming linear scaling would mean 3.5ish hrs just for this nested loop.
Any help is Appreciated. I can provide any other necessary information if needed.
  5 个评论
David Goodmanson
David Goodmanson 2017-3-14
编辑:David Goodmanson 2017-3-14
Hello Clayton, Certainly an easy improvement just to start with is to take the lookups of se_dist.id(k) and two other variables out of the inner for loop (Matlab may be smart enough to do that on its own these days, I guess this will find out). Further improvement is definitely in the cards but you can take a look and see what this does timewise.
for k = 1:h
idk = se_dist.id(k)
evidk = se_dist.evid(k)
distk = se_dist.dist(k)
for n = 1:r
if strcmp(idk,ptime_in.sta(n))==1 && evidk ==ptime_in.evid(n)
dist_in(n) = distk;
id = find(dist_in~=0); % needed?
disp(length(id)); % needed?
end
end
end
Greg
Greg 2017-3-15
dist_in(n) = se_dist.dist(k);
This will overwrite the Nth value of dist_in if subsequent iterations through k satisfy the if logic. Is this truly the output you desire?

请先登录,再进行评论。

采纳的回答

Greg
Greg 2017-3-15
编辑:Greg 2017-3-15
I didn't run your data through to verify, but I'm pretty sure you can kill the inner loop. Although, the fact that nobody else has suggested this makes me feel like I'm missing a piece. For the record, strcmp() already returns logical, so the ==1 is unnecessary, and slower.
for k = 1:h
blnMatch = strcmp(se_dist.id{k},ptime_in.sta) & se_dist.evid(k)==ptime_in.evid
dist_in(blnMatch) = se_dist.dist(k);
end
  1 个评论
Clayton Brengman
Clayton Brengman 2017-3-15
Thank you all for your input. Prior to Greg's answer I think i was getting close with the bsxfun command however the solution he proposed works as intended and is quite quick (~2.2s). Thank you for the responses.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by