In: Computer Science
For this assignment, using MATLAB you are to read from an Excel file “theInputFile.xlsx” an undetermined number of rows and columns.
The first task of your program is to find out if the data in the file is in a square matrix. If it is not square the program will give us a statement telling us the data is not complete.
If it is square then the program proceeds and will find the average of the rows one row at a time using your average function that you will create. However, for a row average to be considered valid the row average must be greater than or equal to the average of the diagonal of the matrix.
(Going from the upper left-hand corner to the lower right-hand corner)
The output should look like the following... either a statement must be outputted stating the data is not complete or if it is complete it will look like the following.
Error using test (line 8)
Something is wrong
or
The average for row 1 is 23.5
The average for row 2 is 3.4
The average for row 3 is not Valid
The average for row 4 is 12.8
.
.
.
Code to copy along with screenshots of code and output are provided.
Please save the code to matlab script file before
executing.
Please refer to screenshots to understand the indentation of
code.
If you have any doubts or issues. Feel free to ask in
comments
Please give this answer a like, or upvote. This will be very
helpful for me.
================================================================
Screenshots of Code :
"theInputFIle.xlsx" ----- (Please save the excel file
into same folder as matlab script file):
Screenshots of Output :
Code to copy:
clc;
% name of our xlsx file
filename = 'theInputFile.xlsx';
% reading file and storing values into A
A = xlsread(filename);
% calculating length and width of A
[row,column] = size(A);
% if square matrix
if row == column
% call average(A) function
average(A);
else
% otherwise
fprintf("The data int the file is not complete!!");
end
% function to calculate averge of rows
function z = average(X)
z = 0;
% calculating number of rows and columns
[row,column] = size(X);
% declaring diagonal_sum variable
diagonal_sum = 0;
% declaring row_sum variable
row_sum = 0;
% calculating diagonal_sum using for loop
for i = 1:column
diagonal_sum = diagonal_sum + X(i,i);
end
% calculating sum of each row and displaying it using for
loop
for i = 1:column
% calcuating row_sum of row = i
for j = 1:row
row_sum = row_sum + X(i,j);
end
% if row_sum greater than equal to diagonal_sum
if row_sum >= diagonal_sum
fprintf("The average for row %d is %.3f \n", i, row_sum);
else
% otherwise
fprintf("The average for row %d is invalid\n", i);
end
row_sum = 0;
end
end
--------------------------XXXXXXX----------------------------------------------------