Optimize this loop to take less computing time
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Clayton Brengman
el 14 de Mzo. de 2017
Comentada: Clayton Brengman
el 15 de Mzo. de 2017
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 comentarios
David Goodmanson
el 14 de Mzo. de 2017
Editada: David Goodmanson
el 14 de Mzo. de 2017
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
el 15 de Mzo. de 2017
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?
Respuesta aceptada
Greg
el 15 de Mzo. de 2017
Editada: Greg
el 15 de Mzo. de 2017
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
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!