Question

How can I write a function in MATLAB with an input of array1 and an output...

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.

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function custom sort(v) that takes as input a vector v and as output returns...
Write a function custom sort(v) that takes as input a vector v and as output returns the vector w sorted into increasing order. For example, if the input is [−2 1 3 1 5], the output should be [−2 1 1 3 5]. Don’t use the built-in ”sort” function or anything similar. matlab question
Write a MATLAB program to determine the factorial value of an input integer between 1 and...
Write a MATLAB program to determine the factorial value of an input integer between 1 and 30. You may NOT use the built-in factorial function. You must implement it using a for loop. Display the result back to the user with 2 decimal places (yes, 2 decimal places). The result should follow the following format:   The factorial value is *.xx
IN MATLAB: Write a function file that takes a vector as an input and returns another...
IN MATLAB: Write a function file that takes a vector as an input and returns another vector with all repeated elements of the original vector. For example, the vector [3 4 1 0 4 -5 7 3] would return the vector [3 4]. Do not use the built-in function "repelem."
Write a function in Matlab that takes as input the number of iterations k, the size...
Write a function in Matlab that takes as input the number of iterations k, the size n, and a sparse matrix A. Have this function run the Power method for k iterations on an initial guess the vector of 1’s and output the dominant eigenvalue and its corresponding eigenvector. Use only basic programming. Write out or print out your function.
Write a MATLAB function function [ AvgPos, AvgNeg ] = PosNegAverage ( X ) that calculates...
Write a MATLAB function function [ AvgPos, AvgNeg ] = PosNegAverage ( X ) that calculates average of positive, AvgPos, and negative, AvgNeg, elements of array X, using the for‐end loop and if‐elseif‐else‐end selection structure. Do not use build‐in MATLAN functions in calculations. Apply the developed function for the following vector X = [ ‐7, 1, 0, 0, 12, 6, 33.2, ‐7.5 ];
Write a MATLAB function and test bench script code to solve the above simple RL/RC circuits...
Write a MATLAB function and test bench script code to solve the above simple RL/RC circuits by following the instructions noted below. The input signal and impulse response generation should be done in the function. The test bench script should be used only to call the function and for signal plotting purposes. No plotting should be done inside the function itself. Name your function L2_C Instructions: Input voltage ,x(t), can be AC or DC. Consider a variable ‘w1’ which can...
Write a function find square root(x) that takes as input a number x and as output...
Write a function find square root(x) that takes as input a number x and as output returns the square root of x. Your results should converge to at least 6 decimal places. matlab question
using MATLAB Write a function which will take two inputs: an array ? and an integer...
using MATLAB Write a function which will take two inputs: an array ? and an integer ?. Here, 1 <= ? < ?????h(?). The objective is to find a contiguous subarray of length ? with the largest sum, within the array ?. The output of the function will be that largest sum. For example, x = [1 2 -1 3], and k =2. Then there are 3 possible contiguous subarrays. 1st subarray : [ 1 2], sum = 3 2nd...
1. Create a function that calculates N!, where N is an input variable to the function....
1. Create a function that calculates N!, where N is an input variable to the function. Use a for loop and calculate the factorial without using the factorial function built into MATLAB. 2. Prof. Sky wants to retire when his retirement account first reaches or exceeds $2,000,000. He deposits $20,000 at the beginning of every year into the account which pays 9% interest per year, compounded annually. Create a function that calculates how many years Prof Moss will have to...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads data from a file and performs regression analysis using polyfit and polyval. The function shall have the following features: The input arguments shall include the file name (string), a vector of integers for the degrees of polynomial fits to be determined, and an optional plot type specifier (‘m’ for multiple plots, ‘s’ for a single plot - default). The data files will be text...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT