create a matrix from 3 vectors in matlab

You can use the reshape function to create a matrix from the 3 vectors. The first input to reshape is the 3 vectors stacked together, and the second input specifies the number of rows and columns in the resulting matrix. Assuming the vectors are named vec1, vec2, and vec3, the code would look like this:

main.m
vec1 = [1 2 3 4];
vec2 = [5 6 7 8];
vec3 = [9 10 11 12];

mat = reshape([vec1; vec2; vec3], 4, 3);
99 chars
6 lines

This would create a 4x3 matrix mat with the following values:

main.m
1  5  9
2  6 10
3  7 11
4  8 12
32 chars
5 lines

related categories

gistlibby LogSnag