Shortening/reducing computing time of for with while loop
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hello, I have some code set up but it takes a bit too long for my liking to run. May I please have some ideas or suggestions on how to optimize it? "some_function" takes up the most time in this code. Thank you.
subjectlist = importdata('subjectlist.txt')
files = dir('*.txt');
for i = 1:length(subjectlist);
temp = load(files(i).name);
corrtemp = corrcoef(temp);
corrtemp(logical(eye(size(corrtemp)))) = 0;
corrtemp(corrtemp < 0) = 0;
counter = 1;
uniqueedges = (nodes^2)-nodes/2;
for j = .1:.01:.4
loop_limit = 200;
a = corrtemp > 0;
k = 1;
while (nnz(triu(a))/uniqueedges > j && loop_limit > 0)
a = corrtemp > prctile(corrtemp(:),k+5);
k = k + .5;
loop_limit = loop_limit - 1:
end
sparse{i,counter} = corrtemp.*a;
kanye{i,counter} = mean(mean(sparse{i,counter},2));
usher{i,counter} = some_function(sparse{i,counter});
eminem{i,counter} = some_function(sparse{i,counter});
counter = counter + 1;
end
end
2 comentarios
John BG
el 23 de En. de 2017
would you please attach subjectlist.txt, part of it, or a text file with data that you agree it's reasonably close to the data you want to process?
thanks for time and attention, awaiting answer
John BG
Respuestas (1)
Jan
el 28 de En. de 2017
Editada: Jan
el 28 de En. de 2017
Start with a pre-allocation of the output:
% Before the loop:
nSubject = length(subjectlist);
nJ = 31; % length(0.1:0.01:0.4)
sparse = cell(nSubject, nJ)
kanye = cell(nSubject, nJ);
usher = cell(nSubject, nJ);
eminem = cell(nSubject, nJ);
Then use the profiler to find the bottleneck of the code. If this e.g. the load command dur to the slow disk (or network drive?) access, it is not worth to improve the calculations.
If it is prctile, the while loop can be replaced by a smarter binary search, which reduces the number of tests until the limit is found.
0 comentarios
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!