Professional Documents
Culture Documents
MATLAB Lecture4 - 2 PDF
MATLAB Lecture4 - 2 PDF
Lecture # 4_2
MATLAB Basics
• Special variables:
– ans: default variable name for the result
– pi: π = 3.1415926…………
– eps: ∈ = 2.2204e-016, smallest amount
by which 2 numbers can differ.
– Inf or inf: ∞, infinity
– NaN or nan: not-a-number
Variables
Variables
3 4 5
matrixB = 8 9 10
6 7 1
• Try to get values 9, 3 and 1 from the matrix
and save it into three variables.
Accessing Whole Columns
and Rows
• To get a whole column,
use this command:
• varA =
matName(:,colNumber);
• To get a whole row, use
this command:
• varA =
matName(rowNumber,:);
Try it Yourself
• Create this matrix:
3 4 5
matrixB = 8 9 10
6 7 1
• Get all the values from row 3 and save it into a new
variable.
• Get all the values from column 1 and save it into a new
variable.
Creating a Matrix of Zeros
• To create a matrix of zeros, use the zeros command.
• Example: create a 6 X 5 matrix of zeros.
Creating a Matrix of Ones
• To create a matrix of ones, use the ones
command.
• Example: create a 5 X 3 matrix of ones.
• ones(5,3)
Creating a Matrix of Random
Numbers
• To create a matrix of random numbers,
use the rand command.
• Example: create a 4 X 4 matrix of random
numbers.
• rand(4,4)
Getting the Size of the Matrix
• To get the size of the matrix, use the size
command.
• Example: to get the size of matrix aaa.
• [numRow, numCol]
= size(aaa);
Transposing a Matrix
• A transpose operation changes the column
of a matrix into rows, and rows into columns.
• To do a transpose, use the single quote
operator.
Example:
Transposing a
Column Matrix
Finding the Maximum Value
• To find the maximum value for a matrix, use
the max function.
• Example: find the maximum value in matrix
aaa.
• maxVal = max(aaa);
Max finds the
maximum value
in each column
where, A=
Example: flipud
Example: fliplr