Prev Next extend

Extending a Vector

Syntax
Matlab or Octave x(in + 1) = y
C++ x.resize(mn + 1)
x(in) = y

Matlab or Octave
Suppose that x is a @(@ m \times n @)@ matrix, i is an index between one and m, and y is a scalar, the Matlab or Octave syntax
     
x(in + 1) = y
extends x to an @(@ m \times (n+1) @)@ matrix and sets the element with index @(@ (i, n+1) @)@ to have value y.

Example
     function [ok] = extend_ok()
     ok       = true;
     m        = 2;
     n        = 3;
     for i = 1 : m
     	for j = 1 : n
     		x(i, j) = i + j;
     	end
     end
     % -----------------------
     for i = 1 : m
     	x(i, n + 1) = i + (n + 1);
     end
     % -----------------------
     [m, n]   = size(x);
     ok       = ok & (m == 2);
     ok       = ok & (n == 4);
     for i = 1 : m
     	for j = 1 : n
     		ok = ok & (x(i, j) == i + j);
     	end
     end
     return


C++
Suppose that x is an @(@ m \times n @)@ ublas matrix<double>, i is a size_t index between zero and @(@ m - 1 @)@, and y has type double, the C++ syntax
     
x.resize(mn + 1)
     
x(in) = y
extends the matrix x to have size @(@ m \times (n + 1) @)@ and sets the element with index @(@ (i, n) @)@ to have value y.

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


Input File: omh/extend.omh