You are on page 1of 9

# Unless otherwise specified, MATLAB stores calculations in a variable named ‘ans’

...

## The expression on the right of = is assigned to the variable on the left.

When entered x=3+4, MATLAB first evaluates 3+4, then assigns the result 7 to variable X

...

Workspace window (on the right) shows all the variables currently in the workspace

...

Adding a semicolon to the end of a command will suppress the output ( though the comman
will still be executed) as you can see in the Workspace.

When entered a command without a semicolon at the end MATLAB displays the result in the
command window.

...

## (Command Window must be the active window for this to work

...

When you enter just a variable name at the command prompt, MATLAB returns the current
value of that variable

...

letter and contain only letters, numbers, and underscores ( _ ).

...

Command ‘ clear’ can clear all variables (look at the WORKSPACE window to see the
change while entering the command clear)

...

...

## 2.3 Using Built-in Functions and Constants

pi represents 𝜋

(although four decimal places are shown, it is calculated for more precision internally)

...

notation.

e.g:

>> x=pi/2

x=1.5708

y=sin(x)

y=1

...

## Imaginary number i is a build in constant in MATLAB

3.1 Manually Entering Arrays

## meaning it contains 1 row and 1 column

...

You can create arrays with multiple elements using square brackets

e.g.

>> x= [3 5]

x= 3 5

...

When you separate numbers by spaces (or commas) MATLAB combines the numbers into a
row vector (an array with one and multiple columns [(1-by –n), here 1 is the #of rows and n is
the #of columns]).

When you separate them by semicolons, MATLAB creates a column vector (n-by-1)

>> x= [7;9]

x=7

...

You can combine spaces and semicolons to create matrices (arrays with multiple rows and
columns)

## When entering matrices, you must enter them row by row.

>> x= [3 4 5; 6 7 8]

x=345

678
...

## You can perform calculations within the square brackets

>>x=[abs(-4) 4^2]

x=4 16

...

first:last

e.g.

>> y=5:8

y=5 6 7 8

...

## You can specify your own spacing as shown below

e.g.

>> x=20:2:26

x=20 22 24 26

...

If you know the number of elements you want in a vector (instead of the spacing between
each element), you could instead use the

## linspace(first, last, number_of_elements)

It will randomly create a vector with the number of elements you inserted which start and end
at the requested first and last number. Here the space between two consecutive numbers will
be automatically decided.

Note the use of commas (,) to separate inputs to the linspace function

>> x=linspace(0,1, 5)

...

## Both linspace and : operator create row vectors.

However, you can convert a row vector into a column vector using the transport operator
(’)

>> x=1:3;

>> x=x’

x=1

...

You can create column vectors in a single command by creating the row vector and
transporting it all on one line.

>> x=(1:2:5)’

x=1

5
...

## 3.3 Array Creation Functions

MATLAB contains many functions that help you to create commonly used matrices, such as
matrices of random numbers

Note that the 2 in the command rand(2) specifies that the output will be a 2-by-2 matrix of
random numbers

...

zeros ( , )

## x=zeros(6,3) creates a 6-by-3 matrix with all elements containing zeros

You can save variables in your WORKSPACE to a MATLAB specific file format called
MAT-file using save command

## Saves a variable x to a MAT-file named foo.mat

...

...
5.0 Import Tool

Data stored in a text file can be imported to MATLAB WORKSPACE by selecting that file
in current folder, right click select import data

## Data is highlighted in blue

Any missing data in the text file is highlighted in orange and labelled as NaN which stands
for Not a Number

You can chose how to import data, for example as a single metrix

...
...

## 5.1 Indexing into Arrays

You can extract values form an array using row , column indexing

>> x=A(5,7)

Extracts the value in 5th row and 7th column of A and assigns the result to the variable x

...

Use MATLAB keyword end as either a row or column index to reference the last element

>> x=A(end, 2)

## >> x=A(end -1, end-2)

Value stored in the last row-1st and last column-2nd place of the A matrix/array is extracted

...