Shortening/reducing computing time of for with while loop
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
John BG
2017 年 1 月 23 日
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
回答 (1 件)
Jan
2017 年 1 月 28 日
編集済み: Jan
2017 年 1 月 28 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!