Study Notes on Array Traversing and Matrix Constructs in MATLAB
Overview of the Course Structure - No requirement to take the last exam for Engineering 450. - Semesters may finish early for students in the course. - Importance of reading the problem statement before class on Thursdays to prepare. ## Traverse Array Lecture - Previous lecture content relevance: exam study will focus on today's and Thursday's lectures. - Topics covered include:- How to traverse arrays. - Methods to output, slice, augment, and add arrays. ### Key Concepts of Traversing Arrays - Definition of Traversing Arrays: The process of going through each element in an array or searching through the array. - Importance is highlighted for sorting, filtering, and counting processes which will be discussed later. ## PowerPoint Presentation Overview - Emphasis on the slides presented during the class. - A particular slide shows the creation of a row vector using the function rand(1,10):
row_vector = rand(1,10);
- This generates a 1x10 row vector with random numbers between 1 and 10. ## Counting Elements in an Array - Example provided with an array:
myArray = [10, 4, 7, 8, 7, 4, 8, 3, 1, 7];
- Task: Count how many elements are less than 5. The counted answer is 4. - Manual counting was demonstrated as follows:- Evaluating each number against 5 to figure out how many were less (detail of checked numbers provided). ## Implementing the Count Method in MATLAB - Introduction to counting high and low values in an array:
count_high = 0;
count_low = 0;
- Example implementation of incrementing counts based on conditions was discussed:
myArray = [10, 4, 7, 8, 7, 4, 8, 3, 1, 7]; % Example array
count_high = 0;
count_low = 0;
for i = 1:length(myArray)
if myArray(i) < 5
count_low = count_low + 1;
elseif myArray(i) > 5
count_high = count_high + 1;
end
end
% After the loop, count_low would be 4, and count_high would be 5 (10, 7, 8, 7, 8).
- Counting is considered a running total, similar to keeping a scoreboard. ### For Loop Specification - To traverse arrays, a for loop is necessary:- 1D arrays require one for loop. - 2D approaches (like matrices) require two for loops for traversal. - Explanation of needing two loops when evaluating a matrix while looking for a specific element. #### Using Length Function - Use
lengthto determine the number of elements in a vector:
vector_example = [1 2 3 4 5];
vec_length = length(vector_example); % vec_length will be 5
matrix_example = rand(5, 6); % A 5x6 matrix
mat_length = length(matrix_example); % mat_length will be 6 (returns the larger dimension)
- For a matrix e.g., 5x6,
lengthreturns 6 regardless of whether it's a row or column. -lengthprovides the larger dimension, confirming its function to return columns over rows in multipurpose dimensions. ## Sizing and Counting Elements in MATLAB - Utilize the following functions like:-size: gives specific row and column counts (note the parameters):
matrix_example = rand(5, 6);
[rows, cols] = size(matrix_example); % rows will be 5, cols will be 6
rows_only = size(matrix_example, 1); % rows_only will be 5
cols_only = size(matrix_example, 2); % cols_only will be 6
To count elements in formulas, e.g., for an effective vector:
Structure of Conditions with For Loops - A structure is applied where:- If an element is less than 5,
count_lowis incremented. - If an element is greater than 5,count_highis incremented. - Review of the MATLAB syntax and logic was discussed using examples to count instances of conditions. ## Indices in Vectors and Arrays - Discussed the indices for various vector elements (demonstrated with examples):
myVector = [10, 20, 30, 40, 50];
first_element = myVector(1); % Accesses the first element (10)
third_element = myVector(3); % Accesses the third element (30)
- Indices for each number in an example 5-element vector. - Transition between 1D and 2D arrays explained, - Understanding of arrays and indexing necessary for effective MATLAB handling. ## Matrix Creation and Manipulation in MATLAB - Introduction on how to create matrices with for loops, emphasizing on element-wise addition and two-dimensional organization:
% Example of creating a 3x3 matrix and filling it with values based on indices
myMatrix = zeros(3,3); % Initialize a 3x3 matrix with zeros
for r = 1:size(myMatrix, 1)
for c = 1:size(myMatrix, 2)
myMatrix(r, c) = r * c; % Fill element (r,c) with product of row and column index
end
end
% myMatrix would then be:
% [1 2 3]
% [2 4 6]
% [3 6 9]
- Instructors guided through practical examples in class on matrix operations (filling matrices numerically). ### Importance of Matrix Structures - Construction of matrices entails two for loops to process:- Processing is top to bottom, left to right as per MATLAB's order of operation. - Elaborated on Dynamic creation, storage and slicing processes. #### Hands-on Examples in Class using MATLAB - Students were prompted to build matrices and explore random number generation.
dynamic_matrix = rand(3, 4); % Creates a 3x4 matrix with random numbers
- Example implementation of the creation of a matrix by specifying dimensions and observing results. ## Logic behind Matrix Traversal - Discussing of methods to access elements in matrices where backtracking could be necessary. - Highlighted conditions to create backward matrices and various traversal routes in code:- Specific commands and coding guidelines demonstrated. ### Interactive Coding Instructions - Students were encouraged to follow along with coding examples and trials to understand logic application for practical scenarios. - Addressing potential for variation in logic and placement depending on the intended function in programming. ## In-Class Activity - Preparations to evaluate number selections from random matrices upon completion of learning materials discussed in lecture. - General setup procedures were shared:- Role of common programming structures (loops, conditions) in achieving task objectives. - Importance of verifying matrix element counts through multiplication of dimensions, aligning with matrix size rules.