Loren on the Art of MATLAB

Turn ideas into MATLAB


Loren on the Art of MATLAB has been archived and will not be updated.

Should min and max Marry?

Do you ever need both the minimum and maximum values on the same data? Sometimes I do, for example, when I want to tweak the limits of a 2-dimensional plot. So I was wondering whether that is a common task? I also wondered what the overhead is in calling min and max separately, instead of scanning through the data once, looking for both the lowest and highest values.


The Plan

I will explore this idea by creating my own simplified versions of max and min, working only for real vector inputs, not checking for NaNs, and not doing any error checking. Clearly, the code I show is not meant for production but to get to the heart of the algorithm.

Create Data

Here I create a very small test dataset.

n = 10;
x = rand(n,1)
x =

Simplified min function

This function is simple, as advertized.

type mymin
function xmin = mymin(x)
%MYMIN Minimum value of real vector input.
%   MYMIN is a simplified version of MIN that
%   finds the minimum value for
%   real-valued vector.  NaNs are not treated.
xmin = Inf;
for ind = 1:length(x)
    if x(ind) < xmin
        xmin = x(ind);

Test mymin Function

Now I compare the result from mymin to the built-in min function in MATLAB.

xmin = mymin(x);
xminML = min(x);
tf = isequal(xmin, xminML)
tf =

Test mymax Function

Do similar testing with mymax.

xmax = mymax(x);
xmaxML = max(x);
tf = isequal(xmax, xmaxML)
tf =

Larger Data

Now I want to do some timing so I will create a much larger array of data.

n = 3e7;
xL = rand(n,1);
xLmin = mymin(xL);
timeMin = toc
xLmax = mymax(xL);
timeMax = toc
timeMin =
timeMax =

Combined Function

Now let me show you my combined function myminmax that loops through the data the same way mymin and mymax did, but does both calculations in the loop together.

type myminmax
function [xmin, xmax] = myminmax(x)
%MYMINMAX Extreme values of real vector input.
%   MYMINMAX is a simplified version of MIN and 
%   MAX combined and finds the
%   minimum  and maximum values for real-valued vector.
%   NaNs are not treated.
xmin = Inf;
xmax = -Inf;
for ind = 1:length(x)
    if x(ind) < xmin
        xmin = x(ind);
    if x(ind) > xmax
        xmax = x(ind);

First, let's check that we get the expected results.

[xminNew, xmaxNew] = myminmax(x);
tf = isequal([xminNew xmaxNew], [xmin xmax])
tf =

Time Combined Function

And now let's time the combined function.

[xLminNew xLmaxNew] = myminmax(xL);
timeMinmax = toc
timeMinmax =

Compare Total Times

To compare the times, let's look at the sum for the times calling the individual functions vs. calling the combined one.

t2 = timeMin+timeMax;
format long
disp('Adding separate times     Combined Time')
disp([t2 timeMinmax])
Adding separate times     Combined Time
   1.495988735998570   1.061442445897453

Reset format to default

format short

Worth It?

Is it worth having a combined function for min and max from a speed point of view? I don't know. It depends on many things, including how often it's needed, and if finding the min and max values is one of the dominant time consumers in the overall algorithm.

Published with MATLAB® 7.6

  • print


댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.