Time for action – programming the minmax function

  1. Open your text editor and write the following code
    Code Listing 5.1
    function [minx, maxx] = minmax(x)					#1
    									#2
      maxx = max(x);							#3
      minx = min(x);							#4
    									#5
    endfunction								#6
  2. Save the code as minmax.m under the current directory or anywhere in the Octave search path.
  3. To execute the function type the following commands at the Octave command prompt:
    octave:1> a = rand(1, 5)
    
    a =
      0.573584    0.588720    0.112184    0.052960    0.555401
    
    octave:2> [mina, maxa] = minmax(a)
    
    mina = 0.052960
    
    maxa = 0.588720
    

    as we would expect.

What just happened?

From line 1 in Code Listing 5.1, we see that the function name is minmax, the function takes the input x, and returns two outputs minx and maxx . It should be clear what code lines 3 and ...

Get GNU Octave now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.