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

MATLAB: 1)This problem is a continuation of the previous problem. You will want

ID: 3858475 • Letter: M

Question

MATLAB:

1)This problem is a continuation of the previous problem. You will want to add up all of the vectors in the file called "Random_Data.txt", but in addition, you want to save all of the intermediate results. For example, once you add up the first two vectors, write the results into another text file called "Results.txt". The next line should have the intermediate answer of adding the previous result to the third vector, etc... Your Results.txt should contain vector_count - 1 vectors with the final vector being the final answer for adding all of the vectors up in the Random_Data.txt file.

I got the answer to this one with the following code:

%Write the code to complete this solution here. This should be a script and not a function.

%Open the file to read

fileId = fopen('Random_Data.txt','r');

%Read first line of the file

count = fscanf(fileId,'%d',[1 1]);

%Initialize Total variable

Total = zeros(1,5);

% '%d' indicates the datatype.

% If your data is floating point then it to '%f'.

for i=1:count

temp = fscanf(fileId,'%d',[1 5]);

Total = Total + temp;

end

%Close the file

fclose(fileId);

2) Cant figure out part 2, please help!: This problem is a continuation of the previous problem. You will want to add up all of the vectors in the file called "Random_Data.txt", but in addition, you want to save all of the intermediate results. For example, once you add up the first two vectors, write the results into another text file called "Results.txt". The next line should have the intermediate answer of adding the previous result to the third vector, etc... Your Results.txt should contain vector_count - 1 vectors with the final vector being the final answer for adding all of the vectors up in the Random_Data.txt file.

Explanation / Answer

Code :

%Write the code to complete this solution here. This should be a script and not a function.

%Open the file to read

fileId = fopen('Random_Data.txt','r');

%Open the file to write

fileId1 = fopen('Results.txt','w');

%Read first line of the file

count = fscanf(fileId,'%d',[1 1]);

%Initialize Total variable

Total = zeros(1,5);

% '%d' indicates the datatype.

% If your data is floating point then it to '%f'.

for i=1:count

temp = fscanf(fileId,'%d',[1 5]);

Total = Total + temp;

%checking for input read from file is greater than 1

if i>1

%writing total to file

fprintf(fileId1,'%d ',Total);

%adding a new line to file

fprintf(fileId1,' ');

end

end

%Close the file

fclose(fileId);

fclose(fileId1);

Sample file :

5
1 2 3 4 5
5 4 3 2 1
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3

Output file :

6 6 6 6 6
7 7 7 7 7
9 9 9 9 9
12 12 12 12 12

Please provide the contents of sample file if the provided one does not matches your requirement.

Also let me know if you face any difficulty in understanding the code.

Please vote up if satisfied.