You are on page 1of 13

MATLAB expressions consist of:

Numerical values or variables

Logical values or variables

Legal applications of MATLAB functions or operators

A combination of MATLAB expressions

Variables
Variable names:
Must start with a letter.
May contain only letters, digits, and the underscore _.
MATLAB is case sensitive, for example one & ONE are different
variables.
MATLAB only recognizes the first 31 characters in a variable name.

Examples: Ali22B, Cost, X3_f22 and s2Sc6.

Assignment statement:
Variable = number; >> t=1234
Variable = expression; >>t=a+b

Special variables:
ans: default variable name for the result.
pi: = 3.1415926
eps: = 2.2204e-016, smallest value by which two numbers can differ
inf: , infinity
NAN or nan: not-a-number

Commands involving variables:


who: lists the names of the defined variables

whos: lists the names and sizes of defined variables


clear: clears all variables
clear name: clears the variable name
clc: clears the command window

Variables ans is assigned by


MATLAB default.
For example, typing
>>12+2.3*2 or >>12+2.3*2,
yields: ans = 16.6000
>>12+2.3*2; yields: blank
(but the result is saved on the
variable "ans"
(write >>ans see the result of
operation which is 16.6000).
Commas (,) tell MATLAB to
display results
semicolons (; ) suppress
printing.

Variables are assigned numerical


values by typing the expression
directly, for example, typing
>>a = 12+2.3*2

yields: a = 16.6000
The answer will not be displayed
when a semicolon is put at the end of
an expression, for example type
>>a = 12+2.3*2;

MATLAB utilizes the following


arithmetic operators: The
following matrix and array
operations are available in
MATLAB:
+

for

addition

for

subtraction

for

multiplication

for

power

for

transpose

for

left division

for

right division

Comment statements are


preceded by a "%".

A special subclass of often-used MATLAB functions is called operators


Assignment operator (=)
Arithmetic operators (+, -, *, /, ^)
Relational operators (<, <=, = =, ~=, >=, >)
Logical operators (&, |, ~)

Basic data type: Matrix


A matrix is a two-dimensional array
of numbers rows and columns.
A vector is a one-dimensional matrix.
It may have either one row or one
column.
MATLAB is based on matrix and
vector algebra; even scalars
are treated as 1 by 1 matrices.
Therefore, vector and matrix
operations are as simple as
common calculator operations.
The number of entries
(elements or components) is
known as the "length" of the
vector.

The entries must


be enclosed in
square brackets.

Vectors and Matrices:

Vectors can be defined in two ways.


The first method is used for
arbitrary elements:

>>v = [1 3 5 sqrt(49)];
creates a 1x4 vector with elements 1,
3, 5 and 7.
Note that commas could have been
used in place of spaces to separate
the elements ([1,3,5,sqrt(49)]).
Additional elements can be added to
the vector:
>>v(5) = 8;
yields the vector v = [1 3 5 7 8].
Previously defined vectors can be
used to define a new vector.
For example, with v defined above
>>a = [9 10];
>>b = [v a];
creates the vector b = [1 3 5 7 8 9
10].
brackets (e.g. [27 36 41]): Creates vectors.
colon operator (e.g. [0:5:30]): Creates linearly
spaced vectors.
linspace (e.g. linspace(0,100,21)): Creates linearly
spaced vectors.
length (e.g. length([0:5:30])): Finds the length of a
vector.
zeros (e.g. zeros(1,5)): Creates vectors filled with
zeroes.
ones (e.g. ones(1,5): Creates vectors filled with
ones.
sum (e.g. sum([5 3 6 2])): Sums up the contents of
a vector.

sort (e.g. sort([5 3 6 2])): Sorts the contents of a


vector.
mean (e.g. mean([5 3 6 2])): Finds the average of
contents.
Create a row vector x consisting of the numbers in
the ordered set: {1 4 7 10} using the colon
operator.

x = [1:3:10]
Set a variable y to be the length of x.

y = length(x)
Set variable y to be the 1st element of x.

y = x(1)
Set variable y to be the 1st, 2nd, and 3rd elements
of x.

y = x([1,2,3]) OR y = x(1:3)
Set variable y to be the 3rd through the last
element of x - and do so such that your solution
works no matter how long x is.

y = x(3:end)
Set variable y to be the next-to-last and last
element of x - and do so such that your solution
works no matter how long x is.

y = x([end-1,end])
Change the 2nd element of x to be 3.

x(2) = 3
Change the 2nd element of x to be 102 and the 4th
element of x be 205.

x([2,4]) = [102, 205]

Vectors and Matrices:


The second method is used for
creating vectors with equally
spaced elements:
>>t = 0:0.1:10;
creates a 1x101 vector with the
elements 0, .1, .2, .3,...,10.
Note that the middle number
defines the increment.
If only two numbers are given,
then the increment is set to a
default of 1:
>>k = 0:10;
creates a 1x11 vector with the
elements 0, 1, 2, ..., 10.

Creating Matrices
A = [ 100 0 99 ; 7 -1 -25 ]
A=
100

99

-1

-25

The : operator
x = 1 : 7 or x = [ 1 : 7 ] creates the same vector as the command x = [ 1 2 3 4 5 6
7]
y = 0 : 3 : 12

is the same as
y = [ 0 3 6 9 12 ]

y = 0 : 3 : 11

is the same as
y=[0369]

z = 15 : -4 : 3 is the same as
z = [ 15 11 7 3 ]
w = 0 : 0.01 : 2 is the same as
w = [ 0 0.01 0.02 ... 1.99 2.00 ]

A=[1 5 9;4 3 2.5; 0.1 10 3i+1]


A =
1.0000
5.0000
9.0000
4.0000
3.0000
2.5000
0.1000
10.0000
1.0000+3.0000i
A(:,2)=[]
A =
1.0000
9.0000
4.0000
2.5000
0.1000
1.0000 + 3.0000i
A(2,2)=[]
??? Indexed empty matrix assignment is not
allowed.

Array Subscripting / Indexing:

The transpose operator is

Z =[ 1

6]

transZ = Z
transZ =
1

Matrix Manipulation Functions

zeros: Create an array of all zeros

ones: Create an array of all ones

eye: Identity Matrix

rand: Uniformly distributed random numbers

diag: Diagonal matrices and diagonal of a matrix

size: Return array dimensions

fliplr: Flip matrices left-right

flipud: Flip matrices up and down

repmat: Replicate and tile a matrix

transpose (): Transpose matrix

rot90: rotate matrix 90

tril: Lower triangular part of a matrix

triu: Upper triangular part of a matrix

cross: Vector cross product

dot: Vector dot product

det:

inv: Matrix inverse

Matrix determinant

Vectors and Matrices:(Cont.)


EXAMPLE:

>> a=2:3, b=[a 2*a;a/2 a]


a=
2 3
b=
2.0000 3.0000 4.0000 6.0000
1.0000 1.5000 2.0000 3.0000
>> c=[b ; b]
c=
2.0000 3.0000 4.0000 6.0000
1.0000 1.5000 2.0000 3.0000
2.0000 3.0000 4.0000 6.0000
1.0000 1.5000 2.0000 3.0000
>> D=c(2:3, 2:3)
D=
1.5000
3.0000

2.0000
4.0000

You might also like