Matrix Input and Access
This section familiarises you with basic commands to input matrices/vectors and accessing their elements. You should memorise these to speed up your coding.
Basic syntax
To try out the commands on this page just type them directly into the MATLAB Command Window and hit <Enter> to execute them.
Most MATLAB commands follow one of the following syntaxes
The semicolon at the end of a command ;
suppresses the display of the result or the output of the command. Lines can be commented out by starting the line with a percentage sign %
.
Variables
In MATLAB, we store any kind of data (numbers, strings etc.) in variables
. Here are a few examples of how to assign variables.
Vectors and matrices
Vectors and matrices are a special type of variables which can be created, accessed and modified using special commands.
Creating vectors and matrices
We can create row and column vectors either by entering them directly or by transposing vectors.
We can create matrices in a similar fashion by entering them row by row or by concatenating previously entered vectors or matrices.
Accessing matrix and vector elements
We can access elements of a vector by supplying the indices of the values that we want to select in parentheses.
We can access elements of a matrix in a similar fashion by supplying either the indices with respect to the column-wise index or by supplying row and column indices separately.
We can also use the colon-operator :
to select all elements of a specific dimension.
The concepts discussed in this section extend easily to arrays of higher dimensions that 2.
Replacing elements in matrices
We can use the tools from the last section to easily replace individual elements of a matrix or even sub-matrices.
Special vectors and matrices
MATLAB supplies you with a set of functions that allow to create vectors or matrices that have a special form. These functions are often useful to avoid unnecessary repetition of inputs.
linspace and : operator
The linspace
command and the :
operator allow to create row vectors of sequences that are linearly spaced. Consider first the :
operator.
The linspace(x1, x2, N)
command generates a row vector containing a sequence of N linearly spaced points between x1 and x2. If N is omitted, it generates 100 points.
logspace
The logspace operators works the same way as the linspace operator, but it produces a sequence of uniformly spaced values in the log-10 space, i.e. logspace(x1, x2, N) generates N points between 10^x1 and 10^x2.
zeros, ones, nan
These commands create matrices of the supplied dimensions which are filled with 0, 1 or missing values (nan = not a number).
eye
eye(n) creates an identity matrix (i.e. a square matrix with ones on the main diagonal and zeros everywhere else) of dimension .
diag
If X is a matrix, diag(X), returns the column vector which extracts the main diagonal of the matrix X. If x is a vectors, diag(X) returns a square diagonal matrix which has the vector x on its main diagonal
repmat
repmat(A, M, N) replicates the vector/matrix A copy its rows M times and its columns N times.
reshape
reshape transforms a matrix into an matrix and can be used if the number of elements does not change i.e. if . It can e.g. be used to convert a matrix into a vector or vice versa.
Note that for the reshape command, the elements retain their index in the matrix which is counted column-wise.
Last updated
Was this helpful?