# How to make a matrix in Matlab or Octave

To create a matrix in Matlab (or Octave) type this command

**matrix = [ a _{11},a_{12},a_{13}, ... ; a_{21}, a_{22}, a_{23}, ... ]**

The elements of a row of the matrix ( a_{11}, a_{12}, a_{13}, ... ) are separated from each other by a comma (,).

Each row of the matrix is separated from the next row by the semicolon symbol (;)

## Examples

__Example 1 (square matrix)__

To define the 2x2 square matrix

$$ \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} $$

The array is assigned to a variable M

M = [ 1,2 ; 3, 4 ]

The matrix has two rows and two columns.

__Example 1 (rectangular matrix)__

Define a 2x3 rectangular matrix

$$ \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix} $$

The array is assigned to a variable M

M = [ 1,2,3 ; 4,5,6 ]

In this case there are three columns and two rows.

__Example 3 (vector)__

To create a vector

$$ \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} $$

The vector is assigned to a variable v

v = [ 1;2;3 ]

The column matrix (vector) has a single column and three rows.

**Report us an error or send a suggestion to improve this page**