Matlab Class 08
Matlab Class 08
Class 08
Class 07
1. 2D Array1
2. 2D Array2
3. Vectors (Dot Product & Cross Product)
A = [1 2 3; 4 5 6; 7 8 9]
A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
Ex:
Given the following 2D array, what would the value of B?
A(2,3)
Linear Indexing
• Uses a single index to refer to an array element
A = [2 1 5; 6 4 8; 3 9 7]
Ex: What index should we use to get the
number “6” from the above matrix?
1. A(4)
2. A(2)
3. A(6)
4. I don’t know
Ex: Find the minimum value among the elements of a
2D array using linear indexing.
myArray = [1 -2 8 3;
4 6 1 -1;
3 -5 0 2]
Concatenation of 2D Arra
Out-of-range Indices
A = [1 2 3; 4 5 6]
myArray = [1 -2 8;
B = [7 8 9];
4 6 1;
C = [1 2 3; 4 5 6; 7 8 9]
3 -5 0]
Ex:
Ex: How do we concatenate matrices A and B to
myArray(4, 4) = ?
get matrix C?
C = [A; B]; This is right
myArray(4, 4) = 1
C = [A B]; ?
Multi-Element Row Column Single Colon Index
Indexing You can use a single colon index to
access all elements in a row or a column.
A = [1 2 3; 4 5 6; 7 8 9] A = [1 2 3; 4 5 6; 7 8 9]
Ex: How do we get the 2nd row of elements
Ex:
What is the command to access all
by indexing? elements in the 2nd row?
A(2,:)
A(2,1:3) Ans: 4 5 6
A(2,[1,2,3]) Ex:
What is the command to access all
Ans:4 5 6 elements in the 2nd column?
A(:,2)
A([2,5,8]) %linear indexing Ans:2
Ans: 4 5 6
Ex: How do we get the 3nd row of elements by indexing?
5
A([3,6,9]) %linear indexing 8
Ans: 7 8 9
Flattening a 2D array Deleting Individual Array
Elements
Take all the elements of a 2D array
and store them as a single 1D array
A = [1 2 3; 4 5 6; 7 8 9] A = [1 2 3; 4 5 6; 7 8 9]
Ex:
1D Arrays Ex: A = [1 2 3; 4 5 6; 7 8 9]
Ex:A = [1 2 3]; index = A > 5
index = [true false true]; A(index), Ans:7 8 6 9 in a colum
A(index)
vector array form
= [1 3]
Using “end” in 2D Arrays
A = [1 2 3; 4 5 6; 7 8 9]
lastCol = A(:,end)% Access elements of last column: 3
6
9
secondtoLastRow =
A(end-1,:)% Access elements before last row: 4 5 6
Dimensional properties of arrays
Ex: A = [1 2 3; 4 5 6];
length(A) = ? Ans: 3 ndims(A) = ? Ans: 2-dimensions
numel(A) = ? Ans: 6 size(A) = ? Ans: 2 3
Elementary arrays
Ex: If you are given the following matrix A, how do you make a new
matrix B that is the same size as matrix A, but is filled with A = [1 6 7 2; 1 2 7 3; 8 2 9 3];
zeros instead?
B = zeros(size(A)); Ans:0,0,0;0,0,0;0,0,
“fliplr” function: Flips array
“flipud” function: Flips array
upside down
left to right Ex:
Ex:
“tril” function: Returns the lower
“rot90” function: Rotates triangular part of an array
array by 90 degrees counter
Ex:
clockwise around element at
index (1,1)
Ex:
Ex:
Ex:
Ex: >> A = [10 0 0; 0 50 60; 0 80 0];
>> A = [10 0 0; 0 50 60; 0 80 0];
>> [row, col] = find(A==50)
>> nzero_index = find(A)
>> A(nzero_index) row =
nzero_index = 2
ans = col =
1 2
5
10
6
50
8
80
60
“sort” function
Where,
Ex:
Where,
Scalar multiplication
• If you multiply the vector “a” by a scalar “alpha”, the vector “a” gets
stretched. If you multiply the vector “a” by a negative scalar “alpha”,
the vector would go in the opposite direction of “a”.
Addition and subtraction of vectors
• Vectors must have the same number of elements to be added or
subtracted
Matlab functions for dot and cross
products
Dot product
• The dot product of two vectors results in a scalar.
NOTE: The dot product of 2 vectors is zero when the two vectors are perpendicular.
x = ones(m,1);
y = 2*ones(m,1);
cMAT = dot(x,y);
c = 0;
for i = 1:m
c = c + x(i,1)*y(i,1);
end
disp(c)
Cross product
• The cross product of the vectors results in a vector that is
perpendicular to both of the vectors. The magnitude of the cross
product is the area of the parallelogram defined by the vectors.
NOTE: The dot product of two vectors is 0 when the two vectors are parallel.
Ex:
Given the following two vectors, find the cross
product.
*The determinant is a scalar value that can be computed from the elements of a square
matrix and encodes certain properties of the linear transformation described by the
matrix.
Matrix determinant and inverse
𝑨= [ 𝒂
𝒄
𝒃
𝒅 ]
𝒅𝒆𝒕𝒆𝒓𝒎𝒊𝒏𝒂𝒏𝒕 𝒐𝒇 𝑨=¿ 𝑨∨¿ 𝒂𝒅 − 𝒃𝒄
Note: A matrix determinant and inverse
are only defined for square matrices.
𝑨=
[ 𝒂 𝒃
] −𝟏
𝒊𝒏𝒗𝒆𝒓𝒔𝒆 𝒐𝒇 𝑨= 𝑨 =
𝟏
𝒄 𝒅
¿ 𝑨∨ ¿ [ 𝒅
−𝒄 ]
−𝒃 =
𝒂
𝟏
[𝒅
𝒂𝒅 − 𝒃𝒄 − 𝒄
−𝒃 ¿
𝒂 ]
Determinant for 3x3 matrix
Determinant for 4x4 matrix
Where,
Ex:
What is the determinant of A?
[ ]
4 3 5
𝐴= 2 1 3
1 6 4
Ex: Write a code in Matlab to calculate the determinant of matrix A = [1 3 4;5 6 7;6 7 7]:
function D = calcDeterminant(A)
A = [1 3 4;5 6 7;6 7 7];
D = 0;
for i=1:3 Using Command Window:
col_index = 1:3; det(A)
col_index(i) = []; Ans: 10
S = A(2:3,col_index);
DS = S(1,1)*S(2,2) - S(1,2)*S(2,1);
if rem(i,2) == 0
C = -A(1,i)
else
C = A(1,i)
end
D = D + C * DS
end
end
D = 10