Lecture 2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

IMAGE PROCESSING IN

MATLAB
LECTURE 2
How to input a number from a
user?
 My_number=input(‘enter any number’)
How to take a string from a user?
 My_string=input(‘enter your name’, ‘s’)
How to simply display?
 A NUMBER/vector/matrix:
 disp(x)
 or disp([1 2 3 3 0])

 A STRING
 disp(‘hi! How are you’)
 or disp(mystr)
 Where mystr= ‘hi! How are you’
Defining vectors- using colon operator

 Suppose you want to define a row vector like this:


 [1 2 3 4 5 6 7 ………………………….. 100]
 It would be tiring to enter all these values by hand
 You could go for a for loop to define such an array
but Matlab can readily take care of that for you.
 Simply type
 >>my_rowvector=1:1:100

STARTING STEP ENDING


VALUE OF SIZE VALUE OF
VECTOR VECTOR
ARRAY ARRAY
Colon operator
 The colon(:) is one of the most useful operator in MATLAB. It is used
to create vectors, subscript arrays, and specify for iterations.
 If you want to specify an increment value other than one, for example:
 100: -5: 50
 MATLAB executes the statement and returns the following result:
 ans = 100 95 90 85 80 75 70 65 60 55 50
 Let us take another example:
 0:pi/8:pi MATLAB executes the statement and returns the following
result:
 ans = Columns 1 through 7 0 0.3927 0.7854 1.1781 1.5708 1.9635
2.3562 Columns 8 through 9 2.7489 3.1416
See the other uses of colon…
EXCERSISE
 DEFINE AN ARRAY SUCH AS THE ONE
BELOW:
[5 10 15 20 25 30 35 40 45 50]
Vector Indexing
Matrix Indexing
Matrix indexing
 Consider a matrix A. I want to access its 7th element.
 A=
FIRST
 2 ELEMENT
6 9 SECON
D
 4 2 8 ELEME
NT
 3 5 1 NINETH
ELEMEN
 Matlab actually stores its elements
T
in memory in a
sequence like this:
 2, 4, 3, 6, 2, 5, 9, 8, 1
Matrix indexing
 >> A(9) Accesses the 9th element
 ans =
 1
 >> A(6:9) Accesses element numbers from 6 to 9
 ans =
 5 9 8 1
Advanced Indexing 1
Advanced Indexing 2
Defining a column vector
 A simple method to make a column vector using
this method is this:
 Make a row vector
 Take its transpose
 >>my_row=1:1:10
 >>my_column=my_row’
 The symbol ‘ after a matrix tells matlab to take its
transpose.
Matrix handling- the Superpower of Matlab

 Takes less than a second to solve complex


matrices- that’s why best for Image processing.
 Entering a matrix
 What are matrix dimensions?
 Matrix indexing
Accessing the elements of a matrix
 Suppose you have a matrix
 My_matrix=[1 4 7 9
 9 6 3 1]
 To access its element at ith row and jth column:
 >>My_matrix(i, j)
 To access its ith (entire) column:
 >>My_matrix(:, i)
 (:, i) means “all rows and ith column)
 To access its ith (entire) row
 >>My_matrix(i, : )
 (i, : ) means ith row and all columns
Accessing elements
 Suppose you have a matrix
 My_matrix=[1 4 7 9
 9 6 3 1]
 To consider 1st three elements of row 1
 >>My_matrix(1, 1:3)

CONSIDER COLUMN
CONSIDER ROW 1
NUMBER 1 TO 3

 147
EXCERCISE
 MAKE ANY 4X3 MATRIX AND ACCESS ITS
ELEMENTS FROM COLUMN 2 AND 3 THAT
LIE WITHIN ROW 3 AND 4
size() & length()
Mathematical operations
 +
 *
 /
 -
 In Matlab, these operators work the same way for a
matrix as they do for real numbers.
 However if you want to add/subtract etc two vectors
element wise then use a dot before the operator, like this:
 .*, .^ etc
Concatenating a matrix
 m1 =

 1 2
 4 5
 3 7
 m2 =

 70 0
 5 0
 6 4
 m3 =

 70 0
 5 0
 6 4
Concatenating a matrix
 >>new_matrix=[m1 m2 m3]===concencates these
three matrices row wise
 >> [m1 m2 m3]

 ans =

 70 0 70 0 70 0
 5 0 5 0 5 0
 6 4 6 4 6 4
Concatenating a matrix
 >>new_matrix=[m1; m2; m3]===concencates these three matrices column wise
 >> [m1; m2; m3]

 ans =

 70 0
 5 0
 6 4
 70 0
 5 0
 6 4
 70 0
 5 0
 6 4
Transpose()
Transposing a matrix
 matrixx =
 1 2 3
 4 5 5
 6 7 8
 To take transpose, just type
 >> matrixx'
 ans =
 1 4 6
 2 5 7
 3 5 8
Addition and Subtraction
Addition and Subtraction
Element-Wise Functions
Operators: element-wise
Operators: standard
Exercise: Vector Operations
Overwriting a row or column
 Suppose you want to overwrite the ith column of a
matrix A,
 >>A(:, i)=[1; 3; 6]
 Similarly, to overwrite a row:
 A(i, :)=[1 3 6]
 Just be sure that the number of elements in rows/
columns is in accordance with the dimensions of
the matrix!!
While loop
 Keeps on repeating a block of code until a specified
condition is met. E.g

 X=1;

 While x<=100
 X=x+1;
 end
FIND COMMAND
TRY THIS
 >>vec=[1 67 13 7 45 89];
 What is the difference between these two
commands:
 >>a=find(vec==13)
 >> [a b] = find (vec==13)
ALSO TRY THIS……
 Matt=[1 2 3 4 5 5 5 5 5 5 5]
 J=find(Matt==5)
 See J in the workspace.
 How do I know the total number of times that 5
occurs in the matrix?
Random numbers
 >>rand %random decimal no between 0 and 1

 >>randi(k) %gives any random integer less than


or equal to the integer ‘k’

 >>randi([a b]) %gives any random integer


between a and b (a and b included)
The Quit command
 Type this at the command window and press
enter…see what happens?

 >>quit

You might also like