How can I write a function in MATLAB with an input of array1 and an output of maxValue (being the highest value in array 1) without calling the built in function in MATLAB. I ned to use a loop to accomplish this.
MATLAB CODE
%File maxNum.m
%function to find greatest number from an input array
function [maxNum] = maxNum(array)
maxNum=array(1);
%for loop to calculate the greatest number
for i=2:numel(array)
%checking if at array(i) is greatest if yes then
%make maxnum=array(i)
if array(i)>=maxNum
maxNum=array(i);
end
end
return
end
File testMax.m to test the maxNum function
clc;
clear all;
array=[5 8 66 89 9 100 65 201 10 45];
disp('Input array');
disp(array);
n=maxNum(array);
disp('Greatest number among the input array is');
n
OUTPUT
Directions: Please create a file named maxNum.m file in matlab then copy the maxNum() code into it, after then create another file named of your choice and copy the code of testMax.m into it and run the latter file.
Get Answers For Free
Most questions answered within 1 hours.