For this homework assignment, you will write a program that will populate a file
ID: 3602583 • Letter: F
Question
For this homework assignment, you will write a program that will populate a file with an NxM matrix,
then it will read the NxM matrix in from the file and perform some basic mathematical operations on it
(calculate the mode, avg, and find the min, max, and median in the matrix). We will cover file I/O next
week. For your algorithm, it is sufficient to state that you will write a specific thing to a file. For example,
you could say something like:
Write: “Answer is” + var1 to file1
You should have a main script that is responsible for calling each of the functions necessary to
run the program. (Functions and script next week. No algorithm required for script.).
Required Functions
1. populate_matrix – this function does not accept any parameters and does not return anything.
It should create a 2d matrix (use the randi function when you are writing your code next week)
that contains random numbers between 0 and 100. The dimensions should also be generated
randomly. Once the matrix is created, it should write the following to a text file:
a.
first line should contain dimensions (row size, followed by a space, followed by column
size)
b. each subsequent line should contain one row of the matrix until the entire matrix has
been written to file
2. process_file – this function accepts 1 parameter ( a file name – should be the file you created in
your populate_matrix function ) and outputs an NxM matrix (dimensions determined by reading
the file), plus it should output N and M (which are the row and column size). This function
should be able to read in a given file that is correctly formatted, extract all of the numbers in the
file and store them as an NxM matrix and return that matrix plus the dimensions of the matrix
to the user as return arguments.
3. calc_mode – this function should accept an NxM matrix and it should output the mode of the
numbers in the matrix. Do NOT use matlab’s built-in function to achieve this. You must code the
function yourself
a.
if you don’t know what mode is, follow this link:
https://www.mathsisfun.com/mode.html
4. calc_avg – this function should accept an NxM matrix and it should output the average of all of
the numbers in the matrix. Do NOT use matlab’s mean function to do this. You must code this
yourself.
5. find_mmm – this function should accept an NxM matrix and output (The output arguments
should be in this order) the minimum, maximum, and median values found in the matrix. Do
NOT use matlab’s built-in functions to do this. You must write the code to do this yourself.
6. output_results – this function should accept the following parameters (in this order): row_count,
col_count, mode, average, minimum, maximum, median). This function will print the following
items (in this order) into a text file. Each of the items must be on a separate line.
a.
dimensions of matrix:
N
x
M
(where N and M are the actual row/column count of matrix)
b. mode of matrix:
[whatever the mode is]
c.
average of matrix:
[whatever the average is]
d. min:
[whatever the min is]
e. max:
[whatever the max is]
f.
median:
[whatever the median is]
Explanation / Answer
%Creates a 2d matrix(using the randi function) that contains
%random numbers between 0 and 100 and writes the dimenstions
% and matrix to the file
function populate_matrix()
%Create row count randomly
M=randi(10)
%Create column count randomly
N=randi(10)
%Creates a 2d matrix that contains random
%numbers between 0 and 100
Matrix=randi(100,M,N)-1
%Open a file in write mode
fid = fopen('MatrixData.txt','w');
%Write the dimensions first
fprintf(fid, '%d %d ',M,N);
%Write the whole matrix to the file at a time
fprintf(fid, '%d ',Matrix);
%Close the file
fclose(fid);
end
%Accepts the file name and reads the diemsions of the
% matrix and the elements of the matrix from file
%Then returns the dimensions and the matrix
function [Matrix,M,N]=process_file(fileName)
%Open the file in read mode
fid = fopen(fileName,'r');
%Read the row size
M=fscanf(fid, '%d',1);
%read the column size
N=fscanf(fid, '%d',1);
%Read the matrix at a time
Matrix=fscanf(fid,'%d',[M,N]);
end
%Accept an NxM matrix and outputs the mode of the numbers
%in the matrix
function [modeNumber]=calc_mode(Matrix)
%Find the row size(M) and column size(N)
[M,N]=size(Matrix);
%Find the product of M and N
dim=M*N;
%Make the matrix as a linear array or vector
%of size M*N=dim
Mat=reshape(Matrix,[1,dim]);
%Sort the linear array
Mat=sort(Mat);
%Intialize modeCount to 1
modeCount=1;
%Assume intially the first element is the mode
modeNumber=Mat(1);
%Intialize a temparary varaible count to 1
count=1;
%iterate through the linear array and find
% the mode
for i=1:dim-1
if(Mat(i)==Mat(i+1))
count=count+1;
else
if(count>modeCount)
modeCount=count;
modeNumber=Mat(i);
end
count=1;
end
end
if(count>modeCount)
modeCount=count;
count=1;
end
%Print the mode
fprintf('Mode of the matrix =%d ',modeNumber);
end
%Accept an NxM matrix and output the minimum,
%maximum, and median values
function [mi,ma,me]=find_mmm(Matrix)
%Find the row size(M) and column size(N)
[M,N]=size(Matrix);
%Find the product of M and N
dim=M*N;
%Make the matrix as a linear array or vector
%of size M*N=dim
Mat=reshape(Matrix,[1,dim]);
%Sort the linear array
Mat=sort(Mat);
%Frist value is the minimum
mi=Mat(1);
%Last value is the maximum
ma=Mat(dim);
%Find the median
if(mod(dim,2)==0)
me= (Mat(dim/2)+Mat((dim/2)+1))/2;
else
me=Mat((1+dim)/2);
end
printf('Min= %d, Max= %d ,Median= %d ',mi,ma,me)
end
function [Avg]= calc_avg(Matrix)
%Find the row size(M) and column size(N)
[M,N]=size(Matrix);
%Find the product of M and N
dim=M*N;
%Make the matrix as a linear array or vector
%of size M*N=dim
Mat=reshape(Matrix,[1,dim]);
%Find the sum of the elements
s=sum(Mat);
%Find the average
Avg=s/dim;
printf('Average= %f ',Avg);
end
%Accepts row_count, col_count, mode, average, minimum, maximum, median
%and prints them to a text file
function output_results(row_count,col_count,modee,average,minimum, maximum,mediann)
%Open the file in write mode
fid = fopen('Output.txt','w');
%Write the matrix dimensions, mode, average, minimum, maximum,
%median to the text file
fprintf(fid,'dimensions of matrix: %d x %d ',row_count,col_count);
fprintf(fid,'mode of matrix: %s ',modee);
fprintf(fid,'average of matrix: %f ',average);
fprintf(fid,'min – %d ',minimum);
fprintf(fid,'median – %d ',maximum);
fprintf(fid,'max – %d ',mediann);
%Close the text file
fclose(fid);
end
%Main script
populate_matrix();
[Mat,rowSize,colSize]=process_file('MatrixData.txt');
mode1=calc_mode(Mat);
[min1,max1,med1]=find_mmm(Mat);
avg1=calc_avg(Mat);
output_results(rowSize,colSize,mode1,avg1,min1,max1,med1);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.