Computer Applications 2024-2025
1. Vectors in MATLAB
1.1 What is a Vector?
A vector is a 1-dimensional array that can store numeric or character data. Vectors can be either:
• Row vectors: Elements arranged horizontally (e.g., [1, 2, 3]).
• Column vectors: Elements arranged vertically (e.g., [1; 2; 3]).
1.2 Creating Vectors
(a) Manual Entry
rowVec = [1, 2, 3, 4]; % Row vector (comma or space separated)
colVec = [1; 2; 3; 4]; % Column vector (semicolon separated)
(b) Using the Colon Operator (:)
• Creates a sequence with a fixed step size.
• Syntax: start : step : end (default step = 1).
Examples:
v1 = 1:5; % [1, 2, 3, 4, 5]
v2 = 1:2:10; % [1, 3, 5, 7, 9] (step=2)
v3 = 10:-1:5; % [10, 9, 8, 7, 6, 5] (decreasing)
(c) Using linspace and logspace
• linspace(start, end, n) → n linearly spaced points.
• logspace(start, end, n) → n logarithmically spaced points.
Examples:
linVec = linspace(0, 10, 5); % [0, 2.5, 5, 7.5, 10]
logVec = logspace(0, 2, 3); % [1, 10, 100] (10^0, 10^1, 10^2)
1.3 Accessing and Modifying Vector Elements
• MATLAB uses 1-based indexing (first element is at position 1).
• Use parentheses () to access elements.
Computer Applications 2024-2025
Examples:
v = [10, 20, 30, 40, 50];
thirdElement = v(3); % Returns 30
subset = v(2:4); % Returns [20, 30, 40]
v(3) = 100; % Changes 30 → 100
v(end) = 0; % Last element → 0
% The syntax v(:) changes v into a column vector (column major):
v(:)
1.4 Common Vector Operations
Operation Example Result
Length length(v) 5
Sum sum(v) 210
Mean mean(v) 42
Max/Min max(v), min(v) 100, 10
Sort sort(v) [10, 20, 40, 50, 100]
Reverse flip(v) [0, 50, 40, 100, 20, 10]
1.5 Logical Indexing with Vectors
• Extract elements based on conditions.
Example:
v = [5, 10, 15, 20];
idx = v > 10; % Logical mask: [0, 0, 1, 1]
selected = v(idx); % Returns [15, 20]
% Replace elements >10 with -1:
v(v > 10) = -1; % v becomes [5, 10, -1, -1]
2. 2D Matrices in MATLAB
2.1 What is a Matrix?
Computer Applications 2024-2025
A matrix is a 2D array with rows and columns.
Example:
A = [1, 2, 3;
4, 5, 6;
7, 8, 9];
This is a 3×3 matrix:
1 2 3
4 5 6
7 8 9
2.2 Creating Matrices
(a) Manual Entry
M = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % 3×3 matrix
(b) Using Functions
Function Example Description
zeros(m,n) zeros(2,3) [0,0,0; 0,0,0]
ones(m,n) ones(2,2) [1,1; 1,1]
eye(n) eye(3) Identity matrix
rand(m,n) rand(2,2) Random values (0-1)
magic(n) magic(3) Magic square
Examples:
Z = zeros(2, 3); % 2×3 matrix of zeros
R = rand(3, 2); % 3×2 random matrix
I = eye(4); % 4×4 identity matrix
2.3 Accessing Matrix Elements
• Syntax: matrix(row, column)
• Use : to select entire rows/columns.
Examples:
Computer Applications 2024-2025
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
A(2, 3) % Returns 6 (row 2, column 3)
A(2, :) % Returns [4, 5, 6] (entire 2nd row)
A(:, 1) % Returns [1; 4; 7] (entire 1st column)
A(1:2, 2:3) % Submatrix: [2,3; 5,6]
2.4 Modifying Matrices
Examples:
A(2,3) = 100; % Changes 6 → 100
A(:,2) = 0; % Sets entire 2nd column to 0
A(3,:) = [10,20,30]; % Replaces 3rd row
2.5 Matrix Operations
Operation Syntax Example
Addition A + B [1,2]+[3,4] = [4,6]
Subtraction A - B [5,6]-[1,2] = [4,4]
Matrix Multiplication A * B [1,2;3,4] * [5;6] = [17;39]
Element-wise Multiplication A .* B [1,2].*[3,4] = [3,8]
Transpose A' [1,2;3,4]' = [1,3;2,4]
Example:
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = A * B; % Matrix multiplication
D = A .* B; % Element-wise multiplication
2.6 Logical Indexing with Matrices
Example:
M = [1, 2, 3; 4, 5, 6; 7, 8, 9];
M(M > 5) = -1; % Replaces all elements >5 with -1
3. Built-in Functions for Vectors & Matrices
Computer Applications 2024-2025
3.1 Summary of Key Functions
Function Example Description
size(A) size([1,2;3,4]) Returns [2,2] (rows, columns)
length(A) length([1,2,3]) Returns 3 (max dimension)
sum(A) sum([1,2,3]) 6 (total sum)
Sum along dimension dim
sum(A,dim) sum([1 2;3 4],1) → [4 6]
(1=columns, 2=rows)
mean(A) mean([1,2,3]) 2 (average)
max(A) max([1,5,3]) 5 (maximum value)
min(A) min([1,5,3]) 1 (minimum value)
sort(A) sort([3,1,2]) [1,2,3] (ascending)
find(cond) find([1,2,3]>1) [2,3] (indices where true)
reshape(A,m,n) reshape(1:6,2,3) Reshapes into 2×3 matrix
diag(A) diag([1,2,3]) Extracts diagonal
Dimension Comparison Table
Function Example Returns When to Use
numel numel(A) 9 When you need total element count
size [r,c]=size(A) r=3, c=3 When you need dimensions separately
length length(A) 3 Largest dimension (avoid with matrices)
ndims ndims(A) 2 Number of dimensions
Data Validation: Check if a matrix is square:
if size(A,1) == size(A,2)
disp('Matrix is square')
end
3.2 Practical Examples
Computer Applications 2024-2025
(a) Finding Even Numbers in a Matrix
A = [1, 2, 3; 4, 5, 6];
evens = A(mod(A,2) == 0); % Returns [2;4;6]
column_sums = sum(A) % [22, 90, 17]
row_means = mean(A, 2) % [28; 2.6667; 12.3333] (along rows)
total_sum = sum(A(:)) % 129 (sum of all elements)
Examples:
A=[1 80 3; 1 2 5; 20 8 9]
n = numel(A)
[r,c]=size(A)
% If X is a matrix, S is a row vector with the sum over each column.
sum(A)
sum(A,"all")
sum(A,2) % Sum along row
[M,I] = max(A,[],1)
[M1,I1] = max(A)
B = sort(A) %sorts in ascending order each column
B1 = sort(A,2,"descend")
B2 = sort(A,2,"ascend")
cc=A(:); % convert to a vector taking values column by column
B2 = sort(cc,"descend")
X = [1 2 3; 3 3 6; 4 6 8; 4 7 7]
mean(X,1) % mean along column
mean(X,2) % mean along each row
(b) Reshaping a Matrix
B = 1:6;
C = reshape(B, 2, 3); % Converts to 2×3 matrix
(c) Extracting Diagonal Elements
D = [1, 2, 3; 4, 5, 6; 7, 8, 9];
diag(D) % Returns [1;5;9]
Syntax for 2D Arrays
Both functions work element-wise on matrices:
mod(A, B) % Element-wise modulus
rem(A, B) % Element-wise remainder
• A and B can be:
o Scalars (e.g., mod(A, 2)).
Computer Applications 2024-2025
o Matrices of the same size (e.g., mod(A, B)).
o One matrix and one scalar (e.g., rem(A, 3)).
Examples with 2D Arrays
Example 1: Basic Usage with Positive Numbers
A = [5, 12; 15, 8];
B = [2, 5; 3, 4];
mod_result = mod(A, B)
rem_result = rem(A, B)
Explanation:
• For positive numbers, mod and rem give identical results.
• 5 mod 2 = 1 (since 5 ÷ 2 = 2 with remainder 1).
• 12 mod 5 = 2 (since 12 ÷ 5 = 2 with remainder 2).
Check Even/Odd Numbers in a Matrix
M = [1, 2; 3, 4];
even_mask = mod(M, 2) == 0 % Logical mask for even numbers
Examples from lab lecture:
a = 5; % Assign the value 5 to the variable 'a'
b = 10; % Assigns the value 10 to the variable 'b'
c = a + b; % Adds 'a' and 'b' and assigns the result to 'c'
d = a * b; % Multiplies 'a' and 'b' and assigns the result to 'd'
vector = [1, 2, 60, 4, 5]; % Defines a row vector
matrix = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % Defines a 3x3 matrix
element = vector(3); % Accesses the third element of 'vector'
element = matrix(2, 3); % Accesses the element in the second row and third column of 'matrix'
% Vector creation
vector = [1, 2, 3, 4, 5];
% Matrix creation
matrix = [1, 2, 3;
4, 5, 6;
Computer Applications 2024-2025
7, 8, 9];
% Matrix addition
matrix_sum = matrix + matrix;
% Matrix multiplication
matrix_product = matrix * matrix;
% Transpose of a matrix
matrix_transpose = matrix';
% Element-wise multiplication
element_wise_product = matrix .* matrix;
% Slicing
subset_vector = vector(2:4); % Subvector containing elements 2 to 4
subset_matrix = matrix(2:3, 1:2); % Submatrix containing rows 2 and 3, and columns 1 and 2
%%%%%%%%%%%%%%%%%%%
v= [10 20 30 40 50];
v(3:5) = [0 0 0] % Replaces elements 3-5 with zeros
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Logical Indexing
v = [5 10 15 20];
idx = v > 10 % [0 0 1 1] (logical mask)
v(idx)=0 % [15 20] (elements where condition is true)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Finding Elements
find(v > 10) % [3 4] (indices where condition is true)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%builtin functions
v= [10 20 30 40 50];
length(v) % Number of elements (5)
max(v) % Maximum value (50)
min(v) % Minimum value (10)
mean(v) % Average (30)
sum(v) % Sum of elements (150)
sort(v) % Sorts in ascending order
% Create a 5x5 array
a = magic(3)
% Find the factors of 5 using logical indexing
f = a(rem(a, 5) == 0);
% Display the factors of 5
disp('Factors of 5 in the array:');
disp(f);
%%%%%%%%%%%%%%%%%
% Define the array A
A = [1, 2, 3, 4, 5, 6, 7, 8, 9];
% Replace values larger than 2 or less than 6 with 0
A((A > 2) & (A < 6)) = 5;
% Display the modified array
disp(A);
%%%%%%%%%%%%%%%%
Computer Applications 2024-2025
a = [1.1, 2.1, 3.2, 4.5];
INDICES = find(a >= 2 & a <= 4);
a(INDICES)
%%%%%%%%%%%%%%%%
a = [1, 2, 3]; b = [1; 2; 3];
% ERROR
%a * a
a = [1.1, 2.1, 3.2, 4.5];
INDICES = find(max(a(:)));
a(INDICES)
%%%%%%%%%%%%%%%%%%%%%%%%
A = randi([1 8], [3 5])
second_column = A(:, 2);
even_numbers = A(mod(A, 2) == 0);
disp(even_numbers);
v=[50 100 20 25]
% z=find(v>10)
% v(z)=0
%%%%%%%%%%%%%%%%%
asc=sort(v)
desc=sort(v,'descend')
matrix = [100, 2, 3; 40, 5, 6; 70, 8, 9]; % Defines a 3x3 matrix
s_col=sort(matrix,'descend') % 2: along each column
s=sort(matrix,2,'descend') % 2: along each row
% Sort all elements in a matrix
s1=sort(matrix(:),'ascend')
A=[1 80 3; 1 2 5; 20 8 9]
n = numel(A)
[r,c]=size(A)
% If X is a matrix, S is a row vector with the sum over each column.
s1=sum(A)
s_All=sum(A,"all")
s_row=sum(A,2) % Sum along row
A=randi([1 100], [3 4])
idx=mod(A, 5) == 0
%div3=A(mod(A, 5) == 0)