Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A. For instance, the trace of the matrix: 4 0 -2 A=157-1 2 -12 is 13. Write a MA

ID: 2266300 • Letter: A

Question

A. For instance, the trace of the matrix: 4 0 -2 A=157-1 2 -12 is 13. Write a MATLAB function (or script) to read the size and the entries of a matrix A from an input file and to calculate its trace. The following is an example of a possible input file specifying the size and entries of a matrix A: 0.8147 0.2785 0.9572 0.7922 0.67870.7060 0.9058 0.5469 0.4854 0.9595 0.75770.0318 o. 1270 0.9575 0.8003 0.6557 0.74310.2769 0.1270 0.9134 0.9649 0.1419 0.0357 0.3922.. 0.0462 0.6324 0.1576 0.4218 0.8491 0.6555. 0.0971 0.0975 0.9706 0.9157 0.9340 0.1712... 0.8235 Assume that the file has n + 1 lines. The first entry in the file is the size of the matriz n. Your program should read this number first and then use this number to read th matrix. Use in_data.txt" as the name of the input file. Your code should use fopen , fscanf , fgetl , and for . . end statements. Notes: The basic syntax of some of the statements that you would need to write your code are as follows: fid-fopen('filename', 'rt'); string-fgetl(fid); array, number-of-entries-read-f scanf (fid,'%s", number-of-entries-to-be-re fprintf (' Comment or message , %d %g ", variable1, variable2, , ..

Explanation / Answer

fid = fopen("Newfile.m","rt");

n = str2num(fgetl(fid)); %read size of matrix

mat = fscanf(fid,"%d",[n n]) %read matrix from file of size [n n]

sum = 0; %sum will calculate trace

for i = 1:n

sum = sum + mat(i,i);

end

printf("Trace = %d ", sum);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote