Finding maximum and minimum values in arrays

March 26th, 2010 Posted in Examples

Let’s write a function file .m that calculates the difference between the and minimum value in a . The file should return a reply. Open an edit window. Create a function file like the one below.

1
2
3
4
5
6
7
% the m-file is created by MatlabCorner.COM
% The m-file calculates the difference between the largest
% and smallest element in vector q.
function r=range(q) % defines a function file range.m
qmax=max(q); % finds the maximum value in the vector q.
qmin=min(q); % finds the minimum -value in the vector q.
r=qmax-qmin; % gives the value that should be returned.

Save the as range.m. Now please enter a vector q=[ 1 3 5 7 9] in the command window. Run the by simply writing range(q) after the prompt in the command window.

1
range(q) % do not forget to press enter.

Leave a Reply

You must be logged in to post a comment.