Site icon Real Technology Tools

MATLAB Matrix Tutorial: Matrix Multiplication, Definition, and Operation

This article explains the key points of manipulating MATLAB matrices when starting. Specifically, you’ll learn to:

Create a Matrix in MATLAB

Define a Matrix

Let’s start by defining matrices. The first thing to know is that you can separate rows by semi-colons (;) and that you define rows by just placing elements next to one another. Then, use square brackets to mark the beginning and the end of your matrix. For example, let’s say you have the following matrix:

\(m=\begin{pmatrix}1 & 2\\3 & 4\end{pmatrix}\)

You can define it as the following in Matlab:

m = [1 2 ; 3 4];

Dimensions of a Matrix

You can find the dimension of a matrix by using the MATLAB command size. You can determine the number of rows and the number of columns of your matrix with the following:

numberOfRows = size(m, 1); 
numberOfColumns = size(m, 2);

in MATLAB, we get:

MATLAB Matrix: Inverse, Transpose, and Identity Matrix and Extracting Elements

The Transpose MATLAB Function

The Inverse MATLAB Function

You can inverse a matrix by using the inv MATLAB command:

m = [1 2 ; 3 4];
mInverse = inv(m);

By definition, if we multiply those 2 matrices, we should get the identity matrix:

The Identity Matrix In MATLAB

You can define the identity matrix with the eye MATLAB function. For example, if you want to have a matrix function identity of three columns and three rows (\(I_3\)), you can write:

 identityMatrix = eye(3); % identity square matrix 3x3

Extract Data from a Matrix

Sometimes, you will have to extract a row or a column from a matrix. Let’s say you have the following matrix:

\(A = \begin{pmatrix}1 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{pmatrix}\)

Defined in MATLAB as the following:

A = [1 2 3 ; 4 5 6 ; 7 8 9];

Matrix Multiplication In MATLAB

Multipliying Matrices in MATLAB: Square Matrix

Multiplying Matrices With Vectors and Non-Square Matrices

Multiplying Matrices by Elements

Key takeaways:

    1. Create a matrix using the following:
      m = [1 2 ; 3 4]; % separate columns by a space and rows by a semi-colon
    2. Get the dimensions of a matrix using:
      size(m, 1) % number of rows
      size(m, 2) % number of columns
    3. MATLAB matrix: transpose, inverse and identity matrix:
      m = [1 2 ; 3 4];
      mTranspose = m'; % transpose matrix
      mInverse = inv(m); % inverse matrix
      identityMatrix = eye(n); % identity square matrix (nxn)
    4. Extract data from a matrix:
      A = [1 2 3 ; 4 5 6 ; 7 8 9];
      C = A(:,2) % select all rows (:) and only the second column (2)
      R = A(2, :) % select only the second row (2) and all columns (:)
      smallerMatrix = A(1:2, 1:2);
      A(2, :)=[]; % remove the second row
      A(:, 2)=[]; % remove the second column
    5. Multiply a matrix: using A*B or A.*B for element-wise multiplication:
      A*B % classic matrix multiplication
      A.*B % element by element matrix multiplication
Exit mobile version