how can i decrease the running time of for loop
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
muhammad ismat
le 4 Avr 2018
Commenté : muhammad ismat
le 4 Avr 2018
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 commentaire
Guillaume
le 4 Avr 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?
Réponse acceptée
Guillaume
le 4 Avr 2018
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 commentaires
Guillaume
le 4 Avr 2018
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.
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!