Problems with min and max functions

21 views (last 30 days)
Kyle Donk
Kyle Donk on 17 Jan 2020
Commented: Kyle Donk on 17 Jan 2020
I am currently doing a project where I have to make x and y equal to 100 random numbers, and then find the max and min of both x and y. I have a maximum value function and a minimum value function, but for some reason, whenever I run the code, nothing appears in the command window. No errors are showing up, so I am not quite sure why nothing is being displayed.
%Generate two arrays of 100 numbers each
%Process the two arrays and determine:
%The largest and smallest values in x and y:
function max=maxval(x)
if (max)<(x(i))
function min=minval(x)
if min>(x(i))
Kyle Donk
Kyle Donk on 17 Jan 2020
I just got it to work. Thank you for your help!

Sign in to comment.

Accepted Answer

Allen on 17 Jan 2020
Since you are defining your function as having a input x, you do not need to redfine it within your function. You can also achieve what you are asking without having to create any functions since max and min are already built-in to MATLAB, which brings up another point of what to avoid. You should try to stay away from using variables that are already defined as functions.
Unless there is any reason that you need x and y to be 10x10 arrays it would make the use of max and min simplier if you make x and y vector-arrays with 100 elements.
x = randi([-9999,9999],100,1);
y = 2.5+rand([100,1]);
minX = min(x);
maxX = max(x);
minY = min(y);
maxY = max(y);
If you need to create a function to accomplish this task, try the following. It will also easily handle array inputs as well as vectors.
function [Min, Max] = MinMax(value)
% Capitalizing the output variables to prevent unwanted behaviour when using built-in min and max functions.
Min = min(value(:));
Max = max(value(:));
Do the following to use your this function.
x = randi([-9999,9999],10,10);
y = 2.5+rand(10);
[minX, maxX] = MinMax(x);
[minY, maxY] = MinMax(y);

More Answers (0)


Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!