1) Read The Following Matrices From A Microsoft Excel File (Assignment 1.xlsx, in Different Sheets)
1) Read The Following Matrices From A Microsoft Excel File (Assignment 1.xlsx, in Different Sheets)
m1 = xlsread('Assignment 1.xlsx',"Sheet1")
m1 = 3×3
8 4 3
-5 6 -2
7 9 -8
m2 = xlsread('Assignment 1.xlsx',"Sheet2")
m2 = 3×3
-5 0 -1
1 2 -1
-3 4 8
size_m1=size(m1);
size_m2=size(m2);
if size_m1(1)==size_m1(2)
fprintf("M1 is a square matrix.")
else
fprintf("M1 is not a square matrix.")
end
M1 is a square matrix.
if size_m2(1)==size_m2(2)
fprintf("M2 is a square matrix.")
else
fprintf("M2 is not a square matrix.")
end
M2 is a square matrix.
c. If any/all of them are square matrix, read the diagonals for all square matrices and
form a vector
dm1=[];
for i=1:size_m1(1)
for j=1:size_m1(2)
if i==j
dm1(i)=m1(i,j);
end
end
end
1
fprintf("The array of diagonal elements of m1 is")
dm1 %% Array of diagonal elements of m1 for sorting
dm1 = 1×3
8 6 -8
dm2=[];
for i1=1:size_m2(1)
for j1=1:size_m2(2)
if i1==j1
dm2(i1)=m2(i1,j1);
end
end
end
fprintf("The array of diagonal elements of m2 is")
dm2 %% Array of diagonal elements of m2 for sorting
dm2 = 1×3
-5 2 8
d. Find out the highest value and lowest value in the vector
%% For Matrix 1
for g=1:length(dm1)
for h=g+1:length(dm1)
if dm1(g)<dm1(h)
max_1=dm1(h);
min_1=dm1(g);
dm1(h)=dm1(g);
dm1(g)=max_1;
end
end
end
fprintf("The sorted array of diagonal elements of m1 is")
dm1
dm1 = 1×3
8 6 -8
2
%% For Matrix 2
for g2=1:length(dm2)
for h2=g2+1:length(dm2)
if dm2(g2)<dm2(h2)
max_2=dm2(h2);
min_2=dm2(g2);
dm2(h2)=dm2(g2);
dm2(g2)=max_2;
end
end
end
fprintf("The sorted array of diagonal elements of m2 is")
dm2
dm2 = 1×3
8 2 -5
f. Calculate the dot product, cross product and dyadic product of the diagonals
Dot Product
dot_product=0;
for p=1:length(dm11)
dot_product=dot_product+(dm11(p)*dm22(p));
end
disp(['The dot product of the diagonals of the two matrices is ',num2str(dot_product)])
Cross Product
i=0;
for g1=1:length(dm11)+2
i=i+1;
dmc1(g1)=dm11(i);
dmc2(g1)=dm22(i);
if i==length(dm11+2)
i=0;
end
end
dmc1;
3
dmc2;
cross_product=[];
for g2=1:length(dm11)
g3=g2+1;
g4=g2+2;
cross_product(g2)=(dmc1(g3)*dmc2(g4)-dmc1(g4)*dmc2(g3));
end
cross_product
cross_product = 1×3
64 -24 46
Dyadic Product
dyadic_product=[];
for f1=1:length(dm11)
row=[];
a=dm11(f1);
for f2=1:length(dm22)
dyadic_product(f1,f2)=a*dm22(f2);
end
end
dyadic_product
dyadic_product = 3×3
-40 16 64
-30 12 48
40 -16 -64