how can i decrease the running time of for loop
Show older comments
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 Comment
Guillaume
on 4 Apr 2018
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?
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!