I'm fairly new to MATLAB and have a piece of code which creates a distance matrix. To be more precise, it creates a distance matrix D between points that lie on an undirected graph, such that Dij is the shortest path between the points on that graph. This matrix is obviously symmetric (because the graph is undirected) and the following is the code snippet that I use to create it:
D = zeros(size(data,1));
for i = 1:size(data, 1)
for j = 1:size(data, 1)
[D(i, j), ~, ~] = graphshortestpath(G, i, j, 'Directed', false);
end
end
This is obviously very wasteful, since I'm not exploiting the symmetry of the matrix. Is there any way in which I can compute only the upper triangular part of the matrix and then somehow "append" the lower triangular part to it, such that I reduce my computations from n^2 to n^2 / 2?
Any help appreciated,
Jason