DEV Community

sofaki000
sofaki000

Posted on • Edited on

Matlab matrix-basics cheatsheet

Matlab basics

MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB is designed to operate primarily on whole matrices and arrays.

All MATLAB variables are multidimensional arrays, no matter what type of data. A matrix is a two-dimensional array often used for linear algebra.

To create an array with four elements in a single row, separate the elements with either a comma (,) or a space.

a = [1 2 3 4]
result:
a = 1×4

     1     2     3     4
Enter fullscreen mode Exit fullscreen mode

To create a matrix that has multiple rows, separate the rows with semicolons.

a = [1 3 5; 2 4 6; 7 8 10]
a = 3×3

     1     3     5
     2     4     6
     7     8    10
Enter fullscreen mode Exit fullscreen mode

Another way to create a matrix is to use a function, such as ones, zeros, or rand. For example, create a 5-by-1 column vector of zeros.

z = zeros(5,1)

z = 5×1

     0
     0
     0
     0
     0
Enter fullscreen mode Exit fullscreen mode

The matrix operators for multiplication, division, and power each have a corresponding array operator that operates element-wise. For example, raise each element of a to the third power:

a.^3
ans = 3×3

           1          27         125
           8          64         216
         343         512        1000
Enter fullscreen mode Exit fullscreen mode

Dot (.) is used to access each individual element of the matrix.

ex: a.^3 -> take each element of a matrix and raise it to the third power.


Accessing elements in the array

A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
A = 4×4

     1     2     3     4
     5     6     7     8
     9    10    11    12
    13    14    15    16
Enter fullscreen mode Exit fullscreen mode

There are two ways to refer to a particular element in an array. The most common way is to specify row and column subscripts, such as:

A(4,2)= 14
Enter fullscreen mode Exit fullscreen mode

Less common, but sometimes useful, is to use a single subscript that traverses down each column in order:

A(8) = 14
Enter fullscreen mode Exit fullscreen mode

To refer to multiple elements of an array, use the colon operator, which allows you to specify a range of the form start:end. For example, list the elements in the first three rows and the second column of A:

A(1:3,2)
ans = 3×1

     2
     6
    10
Enter fullscreen mode Exit fullscreen mode

The colon alone, without start or end values, specifies all of the elements in that dimension. For example, select all the columns in the third row of A:

A(3,:)
ans = 1×5

     9    10    11    12     0
Enter fullscreen mode Exit fullscreen mode

Clearing environment

  • clc = clears command window
  • clear = Remove items from workspace, freeing up system memory

For reproducibility

  • rng default


When Matlab generates random numbers, they're not truly random; they are based on a pseudo-random number generating algorithm. The rng command controls the seed, or starting point, for this value. The "default" values resets it to the original value that MATLAB starts with; this evolves over time.
You can see this behaviour if you do something like called rand(10,1), then calling rng('default') then repeating the rand command. It will generate exactly the same "random" numbers.

Top comments (0)