Prev Next transpose

Matrix Transpose

Syntax
Matlab or Octave y = x'
C++ y = trans(x)

Matlab or Octave
If x is an @(@ m \times n @)@ matrix,
     
y = x'
sets y to the @(@ n \times m @)@ matrix with @[@ y_{j,i} = x_{i,j} @]@

Example
     function [ok] = transpose_ok()
     ok  = true;
     m   = 2;
     n   = 3;
     x   = rand(m, n);
     % -------
     y   = x';
     % -------
     [r, c] = size(y);
     ok     = ok & (r == n);
     ok     = ok & (c == m);
     for i = 1 : m
     	for j = 1 : n
     		ok = ok & (y(j, i) == x(i, j));
     	end
     end
     return


C++
The corresponding C++ syntax is
     
y = trans(x)
where x and y are ublas matrix<double> objects with sizes @(@ m \times n @)@ and @(@ n \times m @)@ respectively.

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


Input File: omh/transpose.omh