0% found this document useful (0 votes)
13 views

MATLAB Lecture 1

The document provides an introduction to MATLAB, covering topics such as creating and manipulating matrices, common commands and functions, and naming conventions for variables. It describes MATLAB's matrix-based approach and uses for numeric computation and visualization.

Uploaded by

Mstafa Mhamad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

MATLAB Lecture 1

The document provides an introduction to MATLAB, covering topics such as creating and manipulating matrices, common commands and functions, and naming conventions for variables. It describes MATLAB's matrix-based approach and uses for numeric computation and visualization.

Uploaded by

Mstafa Mhamad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction to Matlab

Introduction to MatLab

• MatLab is an interactive, matrix-based system


for numeric computation and visualisation

• MATrix LABoratory

• Used in image processing, image synthesis,


engineering simulation, etc
Matlab rules for naming variables
• Matlab case-sensitive with respect to variable
• All variables must start with a letter
• You may have up to 31characters in a variable.
• Punctuation sign are not allowed in a variable.
• The underscore sign “_” is allowed in a
variable.
• Both letters and digits are allowed in a
variable
Creating Variables

• >> varname = 12
varname =
12

>> SS = 56; N = 4; Tot_Num = SS + N

Tot_Num =
60
• The following are valid matlab variable
assignments
A=1
Speed=1500
B_t= v*Q
Name =‘john Smith’

• These are invalid assignments


2for1=‘yes’
First one=1
MATLAB Special Variable
( do not use as variable names ! )

ans Default variable name for results


pi Value of 
eps Smallest incremental number
inf Infinity
NaN Not a number e.g. 0/0
i and j i = j = square root of -1 // so don't use i in loops
realmin The smallest usable positive real number
realmax The largest usable positive real number
Date give date as a string such as 23-oct-2018
Other MATLAB symbols

>> prompt
... continue statement on next line
, separate statements and data
% start comment which ends at end of line
; (1) suppress output
(2) used as a row separator in a matrix
: specify range
Entering Matrices (1)

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

OR
>> A = [
123
456
789]
Entering Matrices (2)

• To create an NxM zero-filled matrix


>> zeros(N,M)
• To create a NxN zero-filled matrix
>> zeros(N)
• To create an NxM one-filled matrix
>> ones(N,M)
• To create a NxN one-filled matrix
>> ones(N)
MATLAB Command Window

» who
Your variables are:
a b c

» whos
Name Size Bytes Class
a 8x8 512 double array
b 9x9 648 double array
c 9x9 648 double array
Grand total is 226 elements using 1808 bytes
Matrix Addition

» A = [ 1 1 1 ; 2 2 2 ; 3 3 3]
» B = [3 3 3 ; 4 4 4 ; 5 5 5 ]
»A+B

ans =
4 4 4
6 6 6
8 8 8
Matrix Multiplication

» A = [ 1 1 1 ; 2 2 2 ; 3 3 3];
» B = [3 3 3 ; 4 4 4 ; 5 5 5 ];
»A*B
ans =
12 12 12
24 24 24
36 36 36
Matrix - Power

»A^2
ans =
6 6 6
12 12 12
18 18 18

»A^3
ans =
36 36 36
72 72 72
108 108 108
Matrix Transpose

A=
1 1 1
2 2 2
3 3 3

» A'
ans =
1 2 3
1 2 3
1 2 3
Identity Function

>> eye (4)


ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Upper Triangle Matrix
» a = ones(5)
a=
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

» triu(a)

ans =
1 1 1 1 1
0 1 1 1 1
0 0 1 1 1
0 0 0 1 1
0 0 0 0 1
Lower Triangle Matrix
» a = ones(5)
a=
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

» tril(a)

ans =
1 0 0 0 0
1 1 0 0 0
1 1 1 0 0
1 1 1 1 0
1 1 1 1 1
Dot Operator
•A*B

• A.*B

• the dot operator performs element-by element


operations, for “*”, “\” and “/”
The Colon Operator (1)
• This is one MatLab’s most important operators

• 1:10 means the vector


– 1 2 3 4 5 6 7 8 9 10

• 100:-7:50
– 100 93 86 79 72 65 58 51
Some built-in functions
• a = 7; b=cos(a), cosh(a)
• b=0.6570
• C=548.3170
• >> sqrt([1,4;9,16])
• Ans= 1 2
• 3 4
• abs(x)=|x|
• abs(-7)
• Ans = 7
• round(x)- nearest integer
• X=[-5.5 5.5];
• >>round(x)
• Ans = -6 6
• >>factorial(5)
• Ans = 120
• Log(4)
• Ans =1.3863
• Sin(30)
• Ans= -0.9880
Vector Functions
• Other MATLAB functions operate essentially
on vectors returning a scalar value.
- max largest component
- min smallest component
- length length of a vector
- sort sort in ascending order
- sum sum of elements
- mean mean value
End

You might also like