LAB-1-1
LAB-1-1
LAB-1-1
LAB REPORT:
1
1|Page
EXPERIMENT NUMBER: 1
LAB TITLE
Part: a & b
MATLAB Code:
% 1a. Even whole numbers between 19 and 63
A = 20:2:62;
Console Window:
LAB TASK # 2
2|Page
EXPERIMENT NUMBER: 1
1. Let x = [3 -2 0 5 1 9 5 6], compute the following:
Part: a, b, c & d
MATLAB Code:
% Given vector
x = [3 -2 0 5 1 9 5 6];
% Assuming the last digit of the registration number is '5' (replace with actual value)
last_digit_reg_no = ;
% 2a. Add the last digit of Reg. No. to each element of the vector
x_plus_reg_no = x + last_digit_reg_no;
% 2c. Compute the square root of each element (negative numbers will return NaN)
x_sqrt = sqrt(x);
Console Window:
a The vector x is updated by adding the last digit of the registration number (assumed to be 5) to
each element. This results in each element of x being incremented by 5.
b The even-indexed elements of x (at positions 2, 4, 6, etc.) are multiplied by 4. This modifies
only the elements at those specific positions, while the others remain unchanged.
c The square root of each element in x is computed. For non-negative values, the square root is
returned as expected, while negative numbers result in NaN values since square roots of
negative numbers are not real.
d Each element of x is cubed, producing a new vector where each element is raised to the power
of 3.
3|Page
EXPERIMENT NUMBER: 1
LAB TASK # 3
1. Let x = [4 3 9 1] and y = [2 1 9 -3], compute the following:
a) Add the sum of the elements of x to y
b) Raise each element of y to the power specified by the corresponding element of x.
c) Multiply each element in x by the corresponding element in y and store the result in ‘c’.
d) Evaluate y*x’ and interpret the result.
Part: a, b, c & d
MATLAB Code:
% Given vectors
x = [4 3 9 1];
y = [2 1 9 -3];
% 3b. Raise each element of y to the power specified by the corresponding element of x
y_raised_to_x = y.^x;
% 3c. Multiply each element in x by the corresponding element in y and store the result
in 'c'
c = x .* y;
Console Window:
4|Page
EXPERIMENT NUMBER: 1
LAB TASK # 4
1. Find a short MATLAB expression to prepare to following matrix:
2. Given a vector t = [-3:0.2:2] write down the MATLAB expressions that will correctly calculate
a. ln(1 − 𝑡 + 𝑡3)
the following:
Part: 1 & 2
MATLAB Code:
% 1. Prepare matrix B
B = [1 2 3 4 5 6 7;
9 7 5 3 1 -1 -3;
4 8 16 32 64 128 256];
% Given vector t
t = -3:0.2:2;
Console Window:
5|Page
EXPERIMENT NUMBER: 1
LAB
TASK # 5
Construct a vector v=[1 2 3 4 5 6 7 8 9 10], develop an algorithm such that the first element of the
vector is multiplied by length(v), second element by length(v)-1and similarly the last element i.e. 10 is
multiplied by length(v)-9. The final vector should be f=[10 18 24 28 30 30 28 24 18 10]. The algorithm
devised should only use the length of vector v to achieve vector f.
MATLAB Code:
% Given vector v
v = 1:10;
% Initialize vector f
f = zeros(1, length(v));
Console Window:
In Task 5, each element of vector v is multiplied by its reversed position in the vector (e.g., the first
element is multiplied by 10, the second by 9, and so on), resulting in vector f.
6|Page
EXPERIMENT NUMBER: 1
LAB TASK # 6
1. Give a MATLAB expression that use only a single matrix multiplication with Matrix B
(obtained in problem 5) to obtain:
% (Part: b)
last_row_matrix = [0 0 1];
% Perform matrix multiplication to get the last row
last_row = last_row_matrix * B;
7|Page
EXPERIMENT NUMBER: 1
% (Part: c)
% Permutation matrix to swap rows 2 and 3
swap_rows_matrix = [1 0 0;
0 0 1;
0 1 0];
Console Window:
LAB TASK # 7
Given a matrix A, such that:
A=[1 x 3 4 ; 4 5 p 7 ; 3 2 5 m]
Note: In matrix A:
x= Last digit of your Reg No. p= 2nd last digit of your Reg No.
m= Average of your Reg No.
Perform the following operations and show their results in MATLAB:
a) Extract the 3rd column of matrix A and store it in vector B.
b) Extract the 1st and 3rd columns of matrix A and store them in matrix C.
c) Add the 1st and 3rd rows of matrix A together and store the result in vector D.
d) Change the value in the 2nd row and 3rd column of A to +7 (instead of -7) and call the result AA
(do not destroy/change the original A matrix).
e) Create a matrix that contains rows 1 and 3 from A, the second row of AA, and the result of part
(c).
8|Page
EXPERIMENT NUMBER: 1
Solution
Reg# = 016
Therefore,
X=6;
P=1;
M= (0+1+6)/3 = 2.33
Part: a, b, c, d & e
MATLAB Code:
% Define matrix A with x, p, and m from Reg No. 016
Co
x = 6; % Last digit of Reg No.
n p = 1; % Second last digit of Reg No. so
l m = 1; % Average of Reg No. digits e
A = [1 x 3 4;
4 5 p 7;
3 2 5 m];
% b) Extract the 1st and 3rd columns of matrix A and store them in matrix C
C = A(:, [1 3]);
% c) Add the 1st and 3rd rows of matrix A together and store the result in vector D
D = A(1, :) + A(3, :);
% d) Change the value in the 2nd row and 3rd column of A to +7 and call the result AA
AA = A; % Create a copy of A
AA(2, 3) = 7; % Update the 2nd row, 3rd column
% e) Create a matrix that contains rows 1 and 3 from A, the second row of AA, and the
result of part (c)
result_matrix = [A(1, :); AA(2, :); A(3, :); D];
Window:
9|Page
EXPERIMENT NUMBER: 1
10 | P a g e