Prev Next slice

Matrix Slices

Syntax
Matlab or Octave r = r_start : r_stride : r_limit
c = c_start : c_stride : c_limit
x(rc)
C++ slice r = slice(r_startr_strider_len)
slice c = slice(c_startc_stridec_len)
matrix_slice_double (xrc)

Matlab or Octave
If x, y are matrices, r, c are vectors, and the other values below are integer scalars,
     
r = r_start : r_stride : r_limit
     
c = c_start : c_stride : c_limit
     
x(rc) = y
Sets r to the vector of integers starting at r_start, incrementing by r_stride and all of which are less than or equal r_limit. It sets c in a similar fashion and then sets the corresponding sub-set of rows and columns of x to the matrix in y.

Example
     function [ok] = slice_ok()
     ok      = true;
     m       = 3;
     n       = 4;
     x       = (1 : m)' * (1 : n);
     y       = zeros(2, 2);
     % --------------------
     r       = 1 : 2 : 3;
     c       = 1 : 2 : 3;
     x(r, c) = y;
     % --------------------
     [p, q]  = size(x);
     ok      = ok & (m == p);
     ok      = ok & (n == q);
     for i = 1 : m
     	for j = 1 : n
     		if any(i == r) & any( j == c) 
     			ok = ok & x(i, j) == 0;
     		else
     			ok = ok & x(i, j) == i * j;
     		end
     	end
     end
     return


C++
The corresponding C++ syntax is
     slice 
r = slice(r_startr_strider_len)
     slice 
c = slice(c_startc_stridec_len)
     matrix_slice_double (
xrc) = y
where x is a ublas matrix<double> object, r, c are mat2cpp matrix_slice_double objects, and the other values are size_t scalars.

Example
     # include <mat2cpp.hpp>
     bool slice_ok(void)
     {	bool   ok  = true;
     	using namespace mat2cpp;
     
     	size_t i, j, k, m(3), n(4);
     	matrix<double> x(m, n);
     	for(i = 0; i < m; i++)
     	{	for(j = 0; j < n; j++)
     			x(i, j) = i * j;
     	}
     	zero_matrix<double> y(2, 2);
     	// -------------------------
     	slice r = slice(0, 2, 2);
     	slice c = slice(0, 2, 2);
     	matrix_slice_double (x, r, c) = y;
     	// -------------------------
     	ok &= (x.size1() == m);
     	ok &= (x.size2() == n);
     	for(i = 0; i < m; i++)
     	{	for(j = 0; j < n; j++)
     		{ 	bool row_match = false;
     			for(k = 0; k < r.size(); k++)
     				row_match |= (i == r.start() + r.stride() * k);
     			bool col_match = false;
     			for(k = 0; k < c.size(); k++)
     				col_match |= (j == c.start() + c.stride() * k);
     			if( row_match & col_match )
     				ok &= (x(i,j) == 0.);
     			else	ok &= (x(i,j) == double(i * j));
     		}
     	}
     	return ok;
     }


Input File: omh/slice.omh