MATLAB: Build a origin destination matrix from 3 vectors without using vec2mat funct

origin-destination-matrixtransportation engineering

hello everyone, i would know how i can build a origin-destination matrix from 3 vectors: first one is the origin label vector, second the destination label vector and the third one is the distances vector.
A= [[0 0 0;0 1 7;0 2 6;0 3 2;0 4 1;1 0 4;1 1 0;1 2 2;1 3 3;1 4 7;2 0 10;2 1 2;2 2 0;2 3 4;2 4 3;3 0 12;3 1 14;3 2 3;3 3 0;3 4 2;4 0 7;4 1 9;4 2 2;4 3 1;4 4 0]]
for each origin to all destination i have to obtain the corresponding distance value
matrix_dist= [0 7 6 2 1;
4 0 2 3 7;
10 2 0 4 3;
12 14 3 0 2;
7 9 2 1 0]
vec2mat function cannot be used

Best Answer

I don't really understand your explanation as I don't see how vec2mat would be used for what you want, but given your input the desired output can be simply obtained with:
matrix_dist = zeros(max(A(:, [1 2]))+1);
matrix_dist(sub2ind(size(matrix_dist), A(:, 1)+1, A(:, 2)+1)) = A(:, 3)