Hi all,
I would like to know if there is a way to speed up indexing procedure in the following code:
N; % given nx1 matrix with some values
D = inf(n,1);D(1,1) = 0;M = zeros(n,1)while sum(M)~=length(N) D_adj = D; M_index = find(M); D_adj(M_index) = inf; % or any very large number
[~,index_m] = min(D_adj); M(index_m) = 1; FOR LOOP % another code for a FOR loop to update values in matrix D;
end
Essentially, for every iteration within WHILE loop, I update a corresponding element of matrix M (by allocation value of 1). My main problem and what slows down my code is the procedure for computing M_index, updating the corresponding elements in matrix D_adj (by allocation value of inf so that they are not taken into account when computing the min value of all elements in matrix D_adj that have zero value in matrix M), and finding the index (of the min element in D_adj) in matrix D. Do you have any suggestion on how I can improve these operations?
Thanks in advance!
Best Answer