MATLAB

A short description of the post.

Skip Moses https://example.com/norajones
2022-02-07

Overview

Today I am going to attempt to learn MATLAB (Matrix Laboratory). MATLAB is a matrix based language geared towards engineers and scientists. With MATLAB one can

  1. Analyze Data
  2. Develop Algorithms
  3. Create models and applications

My Goals

  1. Familarize myself with basic syntax.
  2. Learn how to write simple loops and functions.
  3. Learn how to import and use scripts.
  4. Learn how to write a matrix to a text file.

I need MATLAB so I can run the algorithm from Dong et. al. and compare results with my version that is implemented in python.

My hope is learning basic MATLAB will give me a clearer understanding of the algorithm.

Plan

  1. I will watch this video, and follow along to accomplish goal 1.
  2. I will write some simple loops and functions for goal 2.
  3. I will put these functions in a script and attempt to run it.
  4. I will get Dong et. al. algorithm running.

Notes

Opening MATLAB for the first time

When you open MATLAB most of the screen will be occupied by the Command Window. This functions much like the terminal with python. Here you can input simple commands. The Current Folder will show your current working directory. Notice this is very similar to RStudio. The Workspace window will show values of variables. The PLOT tab is where graphics will be displayed, and the APP tab will show different apps you can add (similar to adding packages in R).

The Command Window and Workspace will be the most important.

Some common operations

The basic arithmetic opperations for python are +, -, *, and /. The ‘=’ sign is used for assignment.

double clicking the top of a window will enlarge and minimize it.

If we enlarge the **Workspace*, we can right click (control click on macbook) and select several different properties to be displayed.

We can clear our command window by running the command ‘clc’. We can in turn hit the up arrow key to scroll through all previous commands that have been run in a given session.

Running the command ‘whos’ will display the workspace.

If we want to clear our workspace we can use the command ‘clear all’. We can delete a single variable, ‘a’, by the command ‘clear a’.

Understanding Variables

By default, every variable defined in MATLAB will be a matrix. When we assign a single integer to a variable, MATLAB stores this as \(1\times 1\) matrix.

We can create a matrix of arbitrary size by:

‘a = [ 9 8 8;]’

will creat a \(1\times 3\) matrix.

‘a = [ 1 1 1; 2 2 2; 3 3 3;]’

will create the matrix:

\[\begin{bmatrix} 1&1&1\\ 2&2&2\\ 3&3&3\\ \end{bmatrix}\]

If we now expand our workspace, we can click on our variable ‘a’, and see the matrix displayed in an excel-like grid. We can now use some of the other attributes in our workspace like min, max, mean, median, ect.

We can right click our variable in our workspace and preform some useful operations like; rename it, change its value, duplicate it and make various plots if its is the correct size.

We can also save the variable as .mat file.

We can load a .mat file with the ‘load(FILENAME.mat)’.

Different Types of Variables

So far, we have only used numeric variables. We can have character variables char. The command

a = ‘hello world’

will create a \(1\times 11\) character matrix.

We can add a row to matrix, a, with the following:

a(2,:) = a(1,:)

This will output

‘hello world’

‘hello world’

Note: you must make sure the rows are the same size when adding them

We can get around this by using strings.

We can create an empty string with:

a = [string(‘Hi how ar you’) string(‘hello world’)]

output:

a =

1×2 string array

"Hi how ar you"    "hello world"