Prev Next

Maximum Element of a Matrix

Syntax
Matlab or Octave y = max( max(x) )
C++ y = max(x)

Matlab or Octave
If x is an @(@ m \times n @)@ matrix,
     
y = max( max(x) )
sets y to the maximum element in the matrix x. If either m or n is equal to one,
     
y = max(x)
also sets y to the maximum element in the matrix x.

Example
     function [ok] = max_ok()
     ok  = true;
     m   = 2;
     n   = 3;
     x   = rand(m, n);
     % ------------------
     y   = max( max(x) );
     % ------------------
     [m, n] = size(y);
     ok     = ok & (m == 1) & (n == 1); 
     ok     = ok & all( all ( x <= y ) );
     ok     = ok & ~ all( all ( x < y ) );
     return


C++
The following is a C++ implementation of the Matlab or Octave syntax above:
     
y = max(x)
where x is a non-empty (non-zero row and column size) ublas matrix<double> object and y is a double.

Example
     # include <mat2cpp.hpp>
     bool max_ok(void)
     {	bool   ok  = true;
     	using namespace mat2cpp;
     
     	size_t i, j, m(2), n(3);
     	matrix<double> x = rand(m, n);
     	// ---------------
     	double y = max(x);
     	// ---------------
     	bool less = true;
     	for(i = 0; i < m; i++)
     	{	for(j = 0; j < n; j++)
     		{	ok   &= x(i,j) <= y;
     			less &= x(i,j) < y;
     		}
     	}
     	ok &= ! less;
     	return ok;
     }


Source
The file max.cpp contains the C++ source code for this operation.
Input File: omh/max.omh