To concatenate multiple vectors into one matrix in MATLAB, we can use the horzcat
or vertcat
functions.
Assume you have three row vectors a
, b
, and c
:
main.m41 chars4 lines
To concatenate these vectors horizontally (i.e., add them as new columns), we use the horzcat
function:
main.m64 chars2 lines
To concatenate them vertically (i.e., add them as new rows), we use vertcat
function:
main.m61 chars2 lines
Note that the vectors must have the same size in the dimension of concatenation. In the above example, the vectors have size 1x3
, so they can be combined either horizontally or vertically.
The resulting matrix
will be:
main.m67 chars6 lines
where the vectors are added as three columns of the matrix.
gistlibby LogSnag