What is an array?

An array is the fundamental form MATLAB uses to store and manipulate data. An array is a set of data entries stored in rows and columns. We describe and define arrays using their dimension, which is a statement of the number of rows followed by the number of columns in the array. This course will use three types of arrays: scalar, vector, and matrix.

Scalar

A scalar is an array with a single element, which means scalars always have a dimension of 1×1 (i.e., 1 row, 1 column).

    \[ {  A_{1\times1} = 5 } \]

Vector

A vector is a one-dimensional array composed of a single row or a single column of elements.

A row vector always has a dimension 1xn, with 1 row and n columns. The example below has 1 row and 5 columns.

    \[   A_{1\times5} =   \left[ {\begin{array}{ccccc}     7 & 2 & 15 & 14 & 8 \\   \end{array} } \right] \]

A column vector always has a dimension nx1, with n rows and 1 column. The example below has 3 rows and 1 column.

    \[   A_{3\times1} =   \left[ {\begin{array}{c}     4 \\     7 \\     11\\   \end{array} } \right] \]

Matrix

A matrix is a two-dimensional array composed of elements arranged in rows and columns. A matrix will have dimension mxn, with m rows and n columns. The example below has 4 rows and 3 columns.

    \[   A_{4\times3} =   \left[ {\begin{array}{ccc}     3 & 8 & 1  \\     0 & 3 & 0 \\     2 & 9 & 7 \\     5 & 4 & 6 \\   \end{array} } \right] \]

Define Arrays in MATLAB

There are many ways to create array variables in MATLAB. You can copy any of the commands below into MATLAB to see how they work.

Create row vector with defined elements

Define each element in the vector individually. You can choose to use commas or spaces to separate the elements in a row vector. Note that rowvec1 and rowvec2 create the identical row vectors.

% define row vectors
rowvec1 = [0 4 2 5]
rowvec2 = [0, 4, 2, 5]

Create column vector with defined elements

The syntax to create a column vector is similar to creating a row vector. The only difference is you must use semicolons between the elements.

% define column vector
colvec = [1; 8; 1; 4]

Create evenly spaced vector of sequential values

You can use MATLAB vector notation and built-in functions to create vectors of evenly spaced values.

% create a row vector from 1 to 5 with an increment of 1 (vec1 = 1 2 3 4 5)
vec1 = 1:5

% create a row vector from 1 to 11 with an increment of 2 (vec2 = 1 3 5 7 9 11)
vec2 = 1:2:11

% use the linspace command to generate a row vector of
% equally spaced elements from 0 to 10 (vec3 = 0 2 4 6 8 10)
vec3 = linspace(0,10,6)

% create a row vector in descending order (vec4 = 5 4 3 2 1)
vec4 = 5:-1:1

Transpose vectors

Transposing vectors changes them from row to column or vice versa. You can use the apostrophe character to transpose from a row to a column or from a column to a row.

% transpose a pre-defined vector (using row vector vec1 as defined above)
row2col_vec1 = vec1'

% transpose a vector during definition
col2row = [9; 8; 7; 6]'

Create matrices

In MATLAB, matrices combine the rules of row and column vectors. The elements within a row are separated by commas or spaces. The rows are separated by semicolons. Note that mat1 and mat2 create identical matrices.

% define a 3x2 matrix
mat1 = [1 3; 9 2; 5 6]
mat2 = [1, 3; 9, 2; 5, 6]