0% found this document useful (0 votes)
8 views7 pages

Laboratory Report 2M

The document outlines a laboratory activity focused on introducing MATLAB, specifically for encoding and performing operations on matrices. It includes objectives, procedures for creating and manipulating matrices, and activities for practical application of matrix operations. The document is prepared by Engr. Hazel Mhay P. Ducusin.

Uploaded by

paula mae Miguel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Laboratory Report 2M

The document outlines a laboratory activity focused on introducing MATLAB, specifically for encoding and performing operations on matrices. It includes objectives, procedures for creating and manipulating matrices, and activities for practical application of matrix operations. The document is prepared by Engr. Hazel Mhay P. Ducusin.

Uploaded by

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

Laboratory Activity No.

2
Introduction to MATLAB
I. Objectives.
a) To encode matrices into MATLAB.
b) To perform matrix operation such as addition, subtraction, multiplication
and division.
c) To access and alter specific elements of a matrix by referring to their
row and column indices.
II. Introduction. (Research your own introduction)

III. Procedures.
1.) Matrices in MATLAB
a.) Creating Matrices in MATLAB
In creating matrices, you can directly specify the elements within square brackets
([]). Elements are separated by spaces or commas within a row, and rows are
separated by semicolons (;).
Example:
Input:

Output:

b.) Creating a Matrix with Random Values


In creating random values of matrix, use rand, randi, or randm to generate
matrices filled with random integers, uniformly distributed numbers, or normally
distributed numbers:
• For Random Integers (with specific range):
A = randi([1, 10], 2, 2); % A 2x2 matrix with random integers from 1 to 10
disp(A);

• Uniformly distributed numbers between 0 and 1:


A = rand(2, 2); % A 2x2 matrix with random numbers between 0 and 1
disp(A);

PREPARED BY: ENGR. HAZEL MHAY P. DUCUSIN


• Normally distributed numbers (mean 0, standard deviation 1):
A = randn(2, 2); % A 2x2 matrix with random numbers from a normal
distribution
disp(A);

• Identity Matrix
An identity matrix is a square matrix with ones on the diagonal and zeros
elsewhere.

• Creating a Diagonal Matrix


You can create a diagonal matrix by typing the command window:

2.) Matrix Operations


a.) Matrix Addition
To add two matrices, they must have the same dimensions. Use the + operator:
Example:
A = [2 5 2; 4 5 6]
B = [2 2 3; 15 11 12]
C = A + B; Adding matrices A and B
disp(C);

PREPARED BY: ENGR. HAZEL MHAY P. DUCUSIN


b.) Matrix Subtraction
C = A - B; Subtracting matrix B from A
disp(C);

c.) Matrix Multiplication


Min matrix multiplication, the number of columns of the first matrix is equal to the
number of rows of the second matrix:
A = [3 5; 2 4];
B = [9 6; 7 8];
C = A * B; % Matrix multiplication
disp(C);

PREPARED BY: ENGR. HAZEL MHAY P. DUCUSIN


d.) Right Matrix Division (/):
For right matrix division use the code:
C = A / B;
disp(C);
e.) Left Matrix Division (\):
For right left matrix division use the code:
C = B \ A;
disp(C);
f.) Element Wise Division
If you want to perform element-wise division of Matrix A by Matrix B, you can use:
C= A./B;
disp(C);
This will divide each corresponding element in Matrix A by the element in the same
position in Matrix B.
g.) Sum of diagonal in Matrices
If you want to find the sum of your matrix A, use the code:
C= sum(diag(A));
disp(C);
You can also change the variable if you want to find the sum of your Matrix B, C and
so on.

PREPARED BY: ENGR. HAZEL MHAY P. DUCUSIN


3.) Modifying Matrix Elements
a.) Accessing Specified Elements:
In accessing specific elements, use row and column indices to access specific
elements in matrix:
A(row, column)
Example:

b.) Modifying specific elements


You can modify a specific element by assigning new values to them.

Modifying entire rows or columns:


• Modifying a row:
A(n, :) = [n];
Example:
A(2, :) = [11, 12, 13]; % Modify the entire 2nd row

• Modifying a column:
A(:, n) = [n];
Example:
A(:, 3) = [14; 15; 16]; % Modify the entire 3rd column

PREPARED BY: ENGR. HAZEL MHAY P. DUCUSIN


Modifying all diagonal elements:
To modify all diagonal elements, here’s an example of replacing all diagonal elements
with a single value (e.g., 22):
Example:

or

Activity 1: Creating Matrices


Create the following matrices in MATLAB:
1.) A 5x5 square matrix with the numbers 11,12,13,14,15 in the first row, 1, 2, 3, 4, 5,
in the second row, 2, 4, 6, 8, 10 in the third row, 10, 9, 8, 7, 6 in the fourth row and
3, 6, 9, 12, 15 in the fifth row.
2.) A 2x3 matrix with a random integer between 1 to 15.
3.) A 5x1 matrix containing the numbers 1, 3, 5, 7, 9.
4.) A 10x9 matrix filled with zeros.
5.) A 5x5 matrix with diagonal numbers from 1 to 5.

PREPARED BY: ENGR. HAZEL MHAY P. DUCUSIN


Activity 2: Applying Matrix Operations
You are given the following two 5x5 matrices:
Matrix A =
3 6 9 12 11
10 8 6 4 2
11 13 15 17 19
6 20 16 13 4
21 23 25 27 29

Matrix B =
5 7 9 11 13
6 8 10 12 14
18 14 1 5 22
16 18 20 22 24
26 27 28 31 33

1.) Add Matrix A and B


2.) Subtract Matrix B from A
3.) Multiply Matrix A and B
4.) Element-wise division of Matrix A by Matrix B

Activity 3: Creating a Matrix (Circle your answer)


1.) Create matrix of size 10x10 filled with random integers between 1 and 100.
2.) Modify the element (position (8,8)) with value of 12
3.) Modify the element position of fifth row and seventh column with the month of your
birthday
4.) Modify the element position of tenth row and tenth column with value of 1
5.) Modify the second row with your highest-grade last semester
6.) Modify the sixth column with your age
7.) Replace the 3rd column with the values 1 to 10.
8.) Modify all the diagonal elements with the value of 24.

IV. Solution: (Print your MATLAB Solution)


V. Conclusion:

PREPARED BY: ENGR. HAZEL MHAY P. DUCUSIN

You might also like