CPA Computer Programming and Applications
(ME-214) Notes
1. Introduction to Arrays
Arrays are used to store and manipulate data in MATLAB.
One-dimensional arrays are called vectors.
Two-dimensional arrays are called matrices.
Arrays can store numbers, characters, or strings.
Key Concepts:
Element: A single value in an array.
Order of an array: Defined by the number of rows (m) and columns (n), denoted as (m, n).
Row Vector: A one-dimensional array with elements arranged in a single row.
Column Vector: A one-dimensional array with elements arranged in a single column.
Matrix: A two-dimensional array with elements arranged in rows and columns.
2. Creating Arrays in MATLAB
Creating Vectors:
Row Vector: Elements are separated by commas or spaces.
matlab
Copy
v = [1, 2, 3, 4]; % Row vector
Column Vector: Elements are separated by semicolons.
matlab
Copy
v = [1; 2; 3; 4]; % Column vector
Colon Operator: Used to create vectors with a specified range and spacing.
matlab
Copy
v = 2:2:10; % Creates a vector [2, 4, 6, 8, 10]
linspace Function: Creates a vector with equally spaced elements.
matlab
Copy
v = linspace(1, 10, 5); % Creates a vector [1, 3.25, 5.5, 7.75, 10]
Creating Matrices:
Matrices are created by separating rows with semicolons.
matlab
Copy
A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % 3x3 matrix
Zeros and Ones Commands: Create matrices filled with zeros or ones.
matlab
Copy
Z = zeros(3, 3); % 3x3 matrix of zeros
O = ones(3, 3); % 3x3 matrix of ones
Identity Matrix: Created using the eye command.
matlab
Copy
I = eye(3); % 3x3 identity matrix
3. Array Addressing and Manipulation
Accessing Elements:
Elements in an array are accessed using indices.
matlab
Copy
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
element = A(2, 3); % Accesses the element in the 2nd row, 3rd column (value = 6)
Modifying Elements:
You can change the value of an element by assigning a new value to its index.
matlab
Copy
A(2, 3) = 10; % Changes the element in the 2nd row, 3rd column to 10
Adding Elements:
Adding elements to a vector:
matlab
Copy
v = [1, 2, 3];
v = [v, 4]; % Adds 4 to the end of the vector
Adding elements to a matrix:
matlab
Copy
A = [1, 2; 3, 4];
A = [A; [5, 6]]; % Adds a new row to the matrix
Deleting Elements:
Use empty brackets [] to delete elements.
matlab
Copy
v = [1, 2, 3, 4];
v(2) = []; % Deletes the 2nd element (result: [1, 3, 4])
4. Built-in Functions for Arrays
Common Array Functions:
Transpose Operator ('): Switches rows and columns.
matlab
Copy
A = [1, 2; 3, 4];
A_transpose = A'; % Transposes the matrix
Size Function: Returns the dimensions of an array.
matlab
Copy
[m, n] = size(A); % Returns the number of rows (m) and columns (n)
Length Function: Returns the length of a vector or the longest dimension of a matrix.
matlab
Copy
len = length(v); % Returns the length of the vector
Sum Function: Sums the elements of an array.
matlab
Copy
total = sum(A); % Sums all elements in the matrix
Max and Min Functions: Find the maximum and minimum values in an array.
matlab
Copy
max_val = max(A); % Finds the maximum value in the matrix
min_val = min(A); % Finds the minimum value in the matrix
5. Introduction to Strings
Strings are arrays of characters.
Strings are created using single quotes.
matlab
Copy
str = 'Hello, World!';
String Functions:
strcat: Concatenates strings.
matlab
Copy
str1 = 'Hello';
str2 = 'World';
result = strcat(str1, ' ', str2); % Result: 'Hello World'
strlength: Returns the length of a string.
matlab
Copy
len = strlength(str); % Returns the length of the string
strcmp: Compares two strings.
matlab
Copy
cmp = strcmp(str1, str2); % Returns 1 if strings are equal, 0 otherwise
6. Important Topics Discussed in Class
Adding Elements to a Vector or Matrix:
Use concatenation to add elements to vectors or matrices.
Example:
matlab
Copy
v = [1, 2, 3];
v = [v, 4]; % Adds 4 to the vector
Deleting Elements:
Use empty brackets [] to delete elements.
Example:
matlab
Copy
v = [1, 2, 3, 4];
v(2) = []; % Deletes the 2nd element
Built-in Functions for Arrays:
Transpose, size, length, sum, max, min, etc.
Introduction to Strings:
Strings are arrays of characters.
Common string functions include strcat, strlength, and strcmp.
7. Practice Problems
Problem 1: Adding Elements to a Vector
Create a vector v = [1, 2, 3] and add the number 4 to the end.
matlab
Copy
v = [1, 2, 3];
v = [v, 4];
Problem 2: Deleting Elements from a Vector
Delete the second element from the vector v = [1, 2, 3, 4].
matlab
Copy
v = [1, 2, 3, 4];
v(2) = [];
Problem 3: Using Built-in Functions
Find the maximum value in the matrix A = [1, 2; 3, 4].
matlab
Copy
A = [1, 2; 3, 4];
max_val = max(A);
Problem 4: String Manipulation
Concatenate two strings str1 = 'Hello' and str2 = 'World'.
matlab
Copy
str1 = 'Hello';
str2 = 'World';
result = strcat(str1, ' ', str2);
EXAMPLE OF BUILTIN FUNCTIONS :