Question
**MATLAB Homework 3: Part 1 Part 2 HELP!!!! Please I need the code for thi ASAP wil rate high and you'll be a lifesaver. I
**MATLAB Homework 3: Part 1 Part 2 HELP!!!! Please I need the code for thi ASAP wil rate high and you'll be a lifesaver. I ALSO included the algorithm from our teacher. Plenty of information given- instructions AND algorithm.
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). 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:
first line should contain dimensions (row size, followed by a space, followed by column size)
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 matlabs built-in function to achieve this. You must code the function yourself
if you dont 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 matlabs 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 matlabs 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.
dimensions of matrix: N x M (where N and M are the actual row/column count of matrix)
mode of matrix: [whatever the mode is]
average of matrix: [whatever the average is]
min: [whatever the min is]
max: [whatever the max is]
median: [whatever the median is]
Deliverables: Script that will call all relevant functions. All functions listed in this doc (1 6)
Algorithm from teacher
populate_matrix
function populate_matrix
N = random number
M = random number
Matrix = array[N x M] of random numbers from 0..100
Open file1
Write N M to file1
for ii from 1 to N
write array[ii, 1..M] to next line in file
process_file
function process_file(fn: name of a file) returns A: array[NxM], N, M
open fn
N = scan first number in file
M = scan second number in file
for ii from 1 to N
A[ii, 1..M] = scan next M numbers in file
close file
calc_mode
function calc_mode(A: array[N x M] of numbers) returns mode of A
linear = convert A to linear array
sorted = sort linear
position = 1
B = array [sorted[1], 1]
for ii from 2 to N x M
if B[position][1] == sorted[ii]
B[position][2] = B[position][2] + 1
else
position = position + 1
B[position][1] = sorted[ii]
B[position][2] = 1
C = size of B
Highest_frequency = B[1][2]
for ii from 2 to C
if B[ii][2] > Highest_frequency
Highest_frequency = B[ii][2]
if Highest_frequency == 1
mode = -1
return
position = 1
mode = array[]
for ii from 1 to C
if B[ii][2] = Highest_frequency
mode[position] = B[ii][1]
position = position + 1
calc_avg
function calc_avg (A: array[N x M]) returns average of all numbers in A
sum = 0;
matrixl = convert A to linear array
matrix_size = N x M
for ii from 1 to matrix_size
sum = sum + matrixl[ii]
avg = sum/matrix_sizel
find_mmm
function find_mmm(A: array [N x M]) returns minimum, maximum, and median values in A
linear = convert A to linear array
sorted = sort linear
matrix_size = N x M
min = sorted[1]
max = sorted[last element]
if matrix_size is even
median = (sorted[matrix_size / 2] + sorted[matrix_size/2 + 1] ) /2
else
median = sorted[ceiling of matrix_size/2]
output_results
function output_results(row_size: number, col_size: number, mode: array[1..n], average: number, min:
number, max: number, med:number)
open file2
print Dimensions of matrix: + row_size + x + col_size to file2
print Mode of matrix: + mode on next line in file2
print Average of matrix: + average on next line in file2
print Min of matrix: + min on next line in file2
print Max of matrix: + max on next line in file2
print Median of matrix: +med on next line in file2
close file
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started