Professional Documents
Culture Documents
1
Question 2...............................................................................................................................................1
Question 3...............................................................................................................................................2
Question 4...............................................................................................................................................2
Question 5...............................................................................................................................................2
Question 6...............................................................................................................................................3
Question 7...............................................................................................................................................3
Question 8...............................................................................................................................................4
Question 9...............................................................................................................................................4
Question 10.............................................................................................................................................4
clear;
clc;
close all;
Question 1
aMat=2*ones(9);
disp(aMat);
2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2
Question 2
%%bMat=zeros(9);%% Generating warning
diagonalMatrix=[1,2,3,4,5,4,3,2,1];
bMat=diag(diagonalMatrix);
disp(bMat);
1 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0
0 0 0 0 5 0 0 0 0
0 0 0 0 0 4 0 0 0
0 0 0 0 0 0 3 0 0
0 0 0 0 0 0 0 2 0
0 0 0 0 0 0 0 0 1
Question 3
%%matrix=zeros(5,3);%% Generating warning
matrix=floor(rand(5,3)*4);
disp(matrix);
2 3 1
1 1 2
0 2 2
1 0 2
0 2 1
Question 4
x=[100, -702, 35; 45, 35, 96];
Sum=sum(x,'all');
Min=min(x,[],'all');
Max=max(x,[],'all');
Mean=mean(x,'all');
Median=median(x,'all');
fprintf('The matrix X is :\n');
disp(x);
fprintf('The Sum of matrix X is :\t%d\n',Sum);
fprintf('The Max of matrix X is :\t%d\n',Max);
fprintf('The Min of matrix X is :\t%d\n',Min);
fprintf('The Mean of matrix X is :\t%d\n',Mean);
fprintf('The Median of matrix X is :\t%d\n',Median);
The matrix X is :
100 -702 35
45 35 96
Question 5
Mat=[3, 4, 5, 6, 7, 8];
ExtractedMatrix=Mat(3:5);
fprintf('The Matrix is :\n');
disp(Mat);
fprintf('The Extracted Matrix is :\n');
disp(ExtractedMatrix);
The Matrix is :
3 4 5 6 7 8
The Extracted Matrix is :
5 6 7
Question 6
total=0;
for i=5:10
total=total +(i^2);
end
fprintf('The Sum of all the squares from 5 to 10 is :\t%d\n',total);
Question 7
RandomMatrix=floor(rand(9,9)*8);
disp(RandomMatrix);
for i=1:9
for j=1:9
if (RandomMatrix(i,j)==7)
RandomMatrix(i,j)=8;
end
end
end
disp(RandomMatrix);
0 0 6 4 0 2 1 0 1
1 7 3 1 1 7 7 1 5
7 0 7 6 1 2 7 5 1
1 6 1 4 3 0 4 5 2
6 6 2 2 0 6 0 5 5
4 6 1 4 7 3 1 3 6
7 0 1 3 7 1 2 4 0
0 3 6 0 3 3 6 2 7
3 2 4 1 3 0 0 5 6
0 0 6 4 0 2 1 0 1
1 8 3 1 1 8 8 1 5
8 0 8 6 1 2 8 5 1
1 6 1 4 3 0 4 5 2
6 6 2 2 0 6 0 5 5
4 6 1 4 8 3 1 3 6
8 0 1 3 8 1 2 4 0
0 3 6 0 3 3 6 2 8
3 2 4 1 3 0 0 5 6
Question 8
C = [-5, 12, 4; 3, -6, 2];
D = [5, -2, 0; 8, 7, 6];
MultipliedMatrix=C.*D;
disp(MultipliedMatrix);
-25 -24 0
24 -42 12
Question 9
E = [1,3,5,7,6,8,0,9,4,2];
SortedArray = sort(E);
disp(SortedArray);
0 1 2 3 4 5 6 7 8 9
Question 10
fprintf('\n\n');
X=[2,4,6,8];
a=11;
b=pi;
Y=abs(X.*exp(a*X)- cos(b*X));
disp(Y);
1.0e+39 *