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

Matlab Class 08

The document covers topics in computer programming related to 2D arrays, including their creation, indexing, and manipulation techniques such as concatenation, deletion, and logical indexing. It also discusses vector operations, including dot and cross products, and matrix properties like determinants and inverses. The content is aimed at students in a mechanical engineering programming course at San Diego State University.

Uploaded by

Jose Aguirre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Matlab Class 08

The document covers topics in computer programming related to 2D arrays, including their creation, indexing, and manipulation techniques such as concatenation, deletion, and logical indexing. It also discusses vector operations, including dot and cross products, and matrix properties like determinants and inverses. The content is aimed at students in a mechanical engineering programming course at San Diego State University.

Uploaded by

Jose Aguirre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

ME 202- Computer Programming and Applications

Class 08
Class 07

1. 2D Array1
2. 2D Array2
3. Vectors (Dot Product & Cross Product)

Louay Alroomi, Ph.D.


Assistant Professor
Department of Mechanical Engineering
San Diego State University
Email: [email protected]
2D Array
• 2D Array has elements arranged into rows and columns

• In mathematics, 2D array is commonly called matrix.


Creating a 2D Array
A = [[1, 2, 3];[4, 5, 6];[7, 8, 9]]

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

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 = [12 15 16 39; 17 3 59 0; 38 1 81 21; 10 49 1


39];
B = A(3,2) Row Column Indexing
Ex: A = [1 2 3; 4 5 6; 7 8 9]
1. 59
2. 1
What index should we use to get the number “6”
3. I don’t know
from the above matrix?

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:

newA = A(:), Ans: the output


is one column vector array
Ex:
What is the command to delete the numbe
column by column “3”?
A(1,3) = []
Replacing Individual Array Elements
x: A = [1 2 3; 4 5 6; 7 8 9] This will generate an ERROR! You cannot
What is the command to replace the delete an element like this because the array
number “3” with “10”? would no longer be rectangular.
A(1,3) = 10
Replacing Rows and Columns using Deleting
Single Rows and Columns using
Colon Operator Single Colon Operator
A = [1 2 3; 4 5 6; 7 8 9]
A = [1 2 3; 4 5 6; 7 8 9]
What is the command to replace all
Ex:
elements in the 2nd row with -1? What is the command to delete all
Ex:
Ans: A(2,:)= -1;
1 2 3
elements in the 2nd row?
-1 -1 -1 Ans: A(2,:)= [];
789 123
What is the command to replace all
Ex: 7 8 9
Ex: elements in the 2nd column with -1?
Ans: A(:,2) = -1 What is the command to delete all
1 -1 3 elements in the 2nd column?
4 -1 6
7 -1 9
Ans: A(:,2) = []
1 3
7 9
Logical Indexing in 2D Arrays
Ex: What is the linear indexing array that
temperature = [73 75; 85 76];
corresponds to row1Index?
row1Index = [true true; false false]; 1. linRow1Index = [true true false false]
col1Index = [true false; true false]; 2. linRow1Index =[true false true false]
3. I don’t know
temperature(row1Index)
Ans: 73 75 % in a column vector array form
temperature(col1Index)
Ans: 73 85 % in a column vector array form
• The use of a logical indexing 2D array returns a
column array.
• The use of a column (row) linear index array will
return a column (row) array.
Logical Indexing in Relational
Arrays
Expressions in 2D

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:

“triu” function: Returns the upper


triangular part of an array
Ex:
“repmat” function: creates
a large array by replicating (tiling)
a smaller array:
Ex:

Ex:

“reshape” function: returns array


reshapeOut with dimensions numRow
× numCols: Ex:
Ex: Ex:
Ex:
“find” function
To find a specific integer value, use relational or equality
operators.

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

• 1D Array: sorts elements in ascending or descending orders


• 2D Array: sorts each column in ascending or descending orders
“sortrows” function
Ex: A = [3 1 3; 1 9 3;4 3 6];

Sort(A) % Arrange each column in the array in ascend order


Ans: [1 1 3;3 3 3;4 9 6]
Sort(A,1) % Arrange each column in the array in ascend order
Ans: [1 1 3;3 3 3;4 9 6]
Sort(A,2) % Arrange each row in the array in ascend order
Ans: [1 3 3;1 3 9;3 4 6]
Sort(A,2, 'descend') % Arrange each row in the array in descend order
Ans: [3 3 1;9 3 1;6 4 3]
Sort(A,2, ‘ascend') % Arrange each row in the array in ascend order
Ans: [1 3 3;1 3 9;3 4 6]
>> A = [9 5 3; 6 8 10; 12 2 1]

>> sortrows(A) % Arrange the first column in ascend order and


transfer all elements in the row of that element in the column
Ans: [6 8 10;9 5 3;12 2 1]

>> sortrows(A, 'descend’) % Arrange the first column in descend


order and transfer all elements in the row of that element in the
column
Ans: [12 2 1;9 5 3;6 8 10]

>> sortrows(A, 2) % Arrange the second column in ascend order and


transfer all elements in the row of that column
Ans: [12 2 1;9 5 3;6 8 10]

>> sortrows(A, 2, 'descend') % Arrange the second column in descend


order and transfer all elements in the row of that column
Ans: [6 8 10;9 5 3;12 2 1]
Ex: Create a function that takes a 2D input matrix that
stores barcode number of fruits in the first column, the
revenue per fruit sold in the second column, and the
total quantity sold per week of each fruit in the third
column. The function should output the barcode number
of the fruit that generates the most revenue per week
and output the total revenue per week for that fruit.
Test Case: function max_rev = findMaxRev(rev)
A = [1234 0.5 25;
rev(:, 4) = rev(:, 2) .* rev(:, 3);
5135 0.7 25; new_rev = sortrows(rev, 4, 'descend');
new_rev
1938 1.2 21; max_rev = new_rev(1, 4);
1382 0.3 35]; end
Vectors
• A vector has a magnitude and direction

• Representation of a vector in 3D space:

a = [a1, a2, a3] or [a1; a2; a3]

• Magnitude of a vector in 3D space:


Ex:

Given the following vector, find the magnitude of the vector a.


Unit vector
• A unit vector has a magnitude of 1

Where,

Ex:

Given the following vector, find it’s unit vector a.

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.

function result = cross_product(v1,v2)


v1 = [5 6 2]
v2 = [1 1 1]
i_mat = [v1(2:3);v2(2:3)]
j_mat = [v1(1) v1(3);v2(1:2:3)]
k_mat = [v1(1:2);v2(1:2)]
i_det = det(i_mat);
j_det = det(j_mat);
k_det = det(k_mat);
result = [i_det -j_det k_det];
end
Matrix inverse
• A square matrix is invertible
• Only if there exists a unique matrix A-1 such that AA-1 = I, where I is the
identity matrix.

• A matrix is not invertible


• If the matrix is singular
• If the determinant* of the matrix is zero.

*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

You might also like