This function works fine.
I call it many times as dijkstra(A,C,1) into an optimization function.
Nevertheless, the Matlab profiler halt me with warnings about the use of sparse functions, and indexing sparse matrices.
I think you could speed up Dirk Stelder's solution by replacing the first for loop (between "candidate=[];' and "[u_index u]=min(candidate);") by the statement
[u_index u] = min(1 ./ (S==0) .* dist);
(Note that u_index is in fact the value; u is the index.) Entries in 1 ./ (S==0) have value Inf when their value in S is 1, i.e. when they have been visited, and 1 otherwise. Multiplying by dist leaves all visited nodes with distance Inf: precisely what 'candidate' looks like.
Comment only