Shortening/reducing computing time of for with while loop

조회 수: 1 (최근 30일)
cwookj
cwookj 2017년 1월 23일
편집: Jan 2017년 1월 28일
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
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
Walter Roberson
Walter Roberson 2017년 1월 23일
What is the code intended to do?

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
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.

카테고리

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