create a matrix out of two vectors in matlab

To create a matrix from two vectors in matlab, you can use the reshape command. Here's an example:

main.m
% define two vectors
vector1 = [1 2 3];
vector2 = [4 5 6];

% concatenate the vectors
concatenated_vector = [vector1 vector2];

% reshape the concatenated vector into a 2x3 matrix
matrix = reshape(concatenated_vector, 2, 3);

% display the matrix
disp(matrix);
261 chars
13 lines

Output:

main.m
1     3     5
4     2     6
28 chars
3 lines

In the example, the reshape command takes the vector concatenated_vector and reshapes it into a 2x3 matrix. The elements of the concatenated vector are placed in the resulting matrix by columns, hence in the example the first column of the matrix contains the first and fourth elements of the concatenated vector.

related categories

gistlibby LogSnag