MATLAB: How can i decrease the running time of for loop

running time

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

Best Answer

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.
Related Question