how can i decrease the running time of for loop

조회 수: 1 (최근 30일)
muhammad ismat
muhammad ismat 2018년 4월 4일
댓글: muhammad ismat 2018년 4월 4일
Dear All, I wrote this code to calculate accuracy of my work
but this code take about 2days to execution when the input is 5000 x 5000 (binary matrix),
so I want to minimize running time of my code.
1-u=max(max(X));
2-result = zeros(u*2,2);
3-ri = 1;
4-for ii=1:u
5-for jj = ii+1:u
6-result(ri,:) = [ii jj];
7-ri = ri+1;
8-end
9-end
10-isRowToRemove = ismember(result,X,'rows');%test result matrix (5000 x 2) is a member in X (data matrix 4000 x 2) or not.
11-result(isRowToRemove,:) = [];
12-cc=result;
13-linindices = sub2ind(size(s), cc(:, 1), cc(:, 2))';% s is matrix(5000 x 5000)
14-b = s(linindices);%calculate the similarity of nonexistence links
15-B=test;
16-linindices = sub2ind(size(s), B(:, 1), B(:, 2))';
17-A = s(linindices);%similarity of test links
18-ndash=sum(arrayfun(@(x) sum(x > b), A));%compare similarity of test and nonexistence links.
19-nddash=sum(arrayfun(@(x) sum(x == b), A));
20-nn=sum(arrayfun(@(x) sum(x < b), A));
21-auc=(ndash + 0.5 * nddash)/(ndash+nddash+nn);
22-Accuracy=max(auc);
suppose i have
X=[1 2
3 4
5 6]
represent the links between nodes 1,2,3,4 and 6.
lines from 1 to 12 calculate the remaining links of a complete network as
results=[1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 5
3 6
4 5
4 6]
then in another code i calculate s (similarity that is about 5000 x 5000) then lines from 13 to 22 compare similarity(s) of b links (portion of X) and result links. this code take very long time about 48 hours without execution when X is about 5000 x 5000 matrix thus i want to minimize execution time
  댓글 수: 1
Guillaume
Guillaume 2018년 4월 4일
Rather than leaving it up to us to decipher your code and understand what it is doing, why don't you explain what it's meant to do, in details?

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

채택된 답변

Guillaume
Guillaume 2018년 4월 4일
Without an explanation of what your code is doing it's near impossible to improve it. Particularly as you haven't explained how X, s and test are related. Saying that:
result = nchoosek(1:u, 2);
should be faster than your double for loop. It's certainly a lot shorter. Also,
ndash = sum(sum(A > b.'));
nddash = sum(sum(A == b.'));
nn = sum(sum(A < b.')); %or nn = numel(A) * numel(b) - ndash - nddash;
should be faster.
I don't understand the point of
cc = result;
B = test;
Why rename the variables? Why can't you use result and test in the rest of the code.
Finally,
Accuracy=max(auc);
is pointless, since auc is guaranteed to be scalar.
  댓글 수: 3
Guillaume
Guillaume 2018년 4월 4일
That code has nothing to do with my answer nor with anything you've posted so far.
As again, you've provided no information about what the code does, what the input variables are, what their sizes are, etc. it's impossible to guess what is wrong.
muhammad ismat
muhammad ismat 2018년 4월 4일
You have already mentioned that in order to executed the basic code, you must have an s matrix and it calculated by another code and called in the basic code. I did not expect that the error come from the code of s so I did not write in the first time. It will be calculated from the following code
for i = 1:n %max(max(X))
for j = 1:i % <-- Note the 1:i instead of 1:n
s(i,j) = (DL(i,IDX(j))*DL(j,IDX(i))) / norm(DL(i,IDX(j))) * norm(DL(j,IDX(i)));
s(j,i) = s(i,j);
end
end
where the input for this code is matrix (5000 x 5000) i.e i= 1:5000 and also j, and each time calculate s(i,j) from mentioned equation that depend on DL (vector 5000 x 2)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by