build a origin destination matrix from 3 vectors without using vec2mat funct

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

2 Comments

"vec2mat function cannot be used"
Why not? If this is homework, then what effort have you put into this other than posting the question on this forum?
vec2mat is able to cut a vector in a determined size to build a full matrix of (nxn) dimension, but if you have some data missing in the OD database the (i,j) position of the element in the matrix suffer a shift. if you have a (3x3) matrix with a missing element, using vec2mat, the answer will be wrong. Given matrix_dist=[ 9 3 2; 4 5 7;11 10 9]
if matrix_dist(1,3) is missing, making use of vec2mat: matrix_dist=vec2mat(matrix_dist,3) produces a irregular solution
matrix_dist =
9 3 4
5 7 11
10 9 0
where (1,3) element induces a global shifting of cutting operation and in (3,3) position the value is wrong and equal to zero

Sign in to comment.

 Accepted 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)

More Answers (0)

Categories

Find more on Communications Toolbox in Help Center and File Exchange

Asked:

on 31 Jan 2017

Edited:

on 31 Jan 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!