Speed up indexing without using a loop
Show older comments
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!
4 Comments
James Tursa
on 15 Mar 2018
In addition to the code, what would be useful to us is a small example of inputs and desired outputs. That way we can more easily see what the end result should be, and maybe think of other ways to do it.
Igor Dakic
on 15 Mar 2018
Edited: per isakson
on 15 Mar 2018
Igor Dakic
on 16 Mar 2018
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing 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!