OKPEDIA MATLAB EN MATRICE

How to make a matrix from many column vectors in Matlab Octave

To create a matrix from many column vectors in Matlab / Octave use horzcat() function

horzcat(v1,v2 [,v3,...])

The v1 and v2 parameters are two or more column vectors.

Alternatively you can use the syntax

[v1,v2]

The result is a two-column matrix.

Examples

Example 1

Create three column vectors

>> v1=[1; 2; 3];
>> v2=[4; 5; 6];
>> v3=[7; 8; 9];

Concatenate vectors in an array using horzcat() function

>> horzcat(v1,v2,v3)
ans =
1 4 7
2 5 8
3 6 9

The horzcat() function concatenates vectors horizontally.

The result is a three column matrix.

$$ \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} \sqcup \begin{pmatrix} 4 \\ 5 \\ 6 \end{pmatrix} \sqcup \begin{pmatrix} 7 \\ 8 \\ 9 \end{pmatrix} \Rightarrow \begin{pmatrix} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{pmatrix} $$

Example 2

Concatenate vectors in an array using syntax [v1,v2,v3]

>> [v1,v2,v3]
ans =
1 4 7
2 5 8
3 6 9

The result is a three column matrix.

https://how.okpedia.org/en/matlab/how-to-make-a-matrix-from-many-vectors-in-matlab-octave


Report an error or share a suggestion to enhance this page


Matrix in Matlab/Octave


FacebookTwitterLinkedinLinkedin