MATLAB: Write a function [M] = theMax(A), where M is the maximum value in an array A. Don’t use the built-in MATLAB function max. Then write a function (M) = theNMax(A,N) where M is an array consisting of the N largest elements in A using the theMax functi

loops

My myMax function looks like this:
function [M] = myMax(A)
for i=1:(length(A)-1)
M=A(i);
if A(i+1)>A(i)
M=A(i+1);
end
end
end
I have no idea how to use this to make [M]=myNMax(A,N).

Best Answer

I'm assuming this is a homework assignment, so I won't give you a full answer. Here are some hints as to how you could do it:
- initialize M
- use myMax(M) to find out if A(i) is greater than the previously stored values in M
- if it is, append the new value to M
- if M has more than N values, delete the first value