0% found this document useful (0 votes)
29 views4 pages

1) Read The Following Matrices From A Microsoft Excel File (Assignment 1.xlsx, in Different Sheets)

The document discusses reading matrices from an Excel file, determining if they are square matrices, finding their diagonal elements, and performing operations on the diagonal elements. It reads two 3x3 matrices from separate sheets, confirms they are square, extracts their diagonal elements into vectors, sorts the vectors in descending order, and calculates the dot, cross, and dyadic products of the diagonal vectors.

Uploaded by

RVCJ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
29 views4 pages

1) Read The Following Matrices From A Microsoft Excel File (Assignment 1.xlsx, in Different Sheets)

The document discusses reading matrices from an Excel file, determining if they are square matrices, finding their diagonal elements, and performing operations on the diagonal elements. It reads two 3x3 matrices from separate sheets, confirms they are square, extracts their diagonal elements into vectors, sorts the vectors in descending order, and calculates the dot, cross, and dyadic products of the diagonal vectors.

Uploaded by

RVCJ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

1) Read the following matrices from a Microsoft Excel file (Assignment

1.xlsx, in different sheets).

a. Read and print the sizes of the matrices

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);

b. Determine if the matrices are square

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

dm11=ones(length(dm1(1))).*dm1; %% Copy of array of diagonal elements of m1 for further calcula

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

dm22=ones(length(dm2(1))).*dm2; %% Copy of array of diagonal elements of m2 for further calcul

d. Find out the highest value and lowest value in the vector

e. Rearrange the vector in a descending order

%% 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")

The sorted array of diagonal elements of m1 is

dm1

dm1 = 1×3
8 6 -8

disp(['The maximum value amongst the diagonal elements of m1 is ',num2str(dm1(1))])

The maximum value amongst the diagonal elements of m1 is 8

disp(['The minimum value amongst the diagonal elements of m1 is ',num2str(dm1(length(dm1)))])

The minimum value amongst the diagonal elements of m1 is -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")

The sorted array of diagonal elements of m2 is

dm2

dm2 = 1×3
8 2 -5

disp(['The maximum value amongst the diagonal elements of m2 is ',num2str(dm2(1))])

The maximum value amongst the diagonal elements of m2 is 8

disp(['The minimum value amongst the diagonal elements of m2 is ',num2str(dm2(length(dm2)))])

The minimum value amongst the diagonal elements of m2 is -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)])

The dot product of the diagonals of the two matrices is -92

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

You might also like