Prev Next

Minimum Element of a Matrix

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

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

Example
     function [ok] = min_ok()
     ok  = true;
     m   = 2;
     n   = 3;
     x   = rand(m, n);
     % ------------------
     y   = min( min(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 = min(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 min_ok(void)
     {	bool   ok  = true;
     	using namespace mat2cpp;
     
     	size_t i, j, m(2), n(3);
     	matrix<double> x = rand(m, n);
     	// ---------------
     	double y = min(x);
     	// ---------------
     	bool greater = true;
     	for(i = 0; i < m; i++)
     	{	for(j = 0; j < n; j++)
     		{	ok      &= x(i,j) >= y;
     			greater &= x(i,j) > y;
     		}
     	}
     	ok &= ! greater;
     	return ok;
     }


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