Matrix manipulations

Many functions are provided for the manipulation of matrices. For instance, it is possible to cast a matrix object into a different type, to concatenate matrices in all dimensions (see cat, etc.), find the non-zero elements, or to transpose it.

all

template<typename T>
matrix<std::size_t> castor::all(matrix<T> const &A)

Linear indexing of all elements.

all(A) return row matrix with all indices of A in linear indexing.

matrix<> A = eye(3,4);
matrix<std::size_t> L = all(A);
matrix<> B = eval(A(L));
disp(B);

See row, col, get, set, view, cview.

cast

template<typename T = double, typename S>
inline matrix<T> castor::cast(matrix<S> const &A, T b = 0)

Cast an array to a different data type.

B = cast(A,T()) or B = cast<T>(A) casts A to newtype T. A must be convertible to type T.

Classical type T are logical, int, float, double (default), etc.

matrix<int>   A = {1,2,3,4};
matrix<long>  B = cast(A,long());
matrix<float> C = cast<float>(A);
auto          D = cast(A);
disp(A);
disp(B);
disp(C);
disp(D);

See Class matrix.

cat

template<typename T = double, typename R, typename S>
inline auto castor::cat(int dim, R const &A, S const &B)
template<typename R, typename S>
inline auto castor::cat(int dim, R A, matrix<S> const &B)
template<typename R, typename S>
inline auto castor::cat(int dim, matrix<R> const &A, S B)
template<typename R, typename S>
auto castor::cat(int dim, matrix<R> const &A, matrix<S> const &B)

Concatenate array.

cat(DIM,A,B) concatenates the arrays A and B along the dimension DIM. cat(1,A,B) is the same as vertcat(A,B). cat(2,A,B) is the same as horzcat(A,B).

matrix<> A = {1,2,3};
matrix<> B = {4,5,6};
matrix<> C = cat(1,A,B);
matrix<> D = cat(2,A,B);
disp(C);
disp(D);

See vertcat, horzcat.

clear

template<typename T>
void castor::clear(matrix<T> &A)

Clear matrix to free memory.

clear(A) removes all values of matrix A and fix size to 0x0. clear should be used to free memory without deleting matrix object.

matrix<> X = {{1,2,3},{4,5,6}};
disp(X);
clear(X);
disp(X);

See Class matrix, resize.

col

template<typename T>
matrix<std::size_t> castor::col(matrix<T> const &A)

Bi-linear indexing of column elements.

col(A) return row matrix with column indices of A in bi-linear indexing.

matrix<> A = eye(3,4);
matrix<std::size_t> J = col(A);
matrix<> B = eval(A(0,J));
disp(B);

See row, all, get, set, view, cview.

find

template<typename T>
matrix<std::size_t> castor::find(matrix<T> const &A)

Find linear indices of nonzero elements.

L = find(X) returns the linear indices corresponding to the nonzero entries of the array X. X may be a logical expression.

matrix<> A = eye(3,4);
matrix<std::size_t> L = find(A);
disp(L);

Use ind2sub(size(X),L) to calculate bi-linear indices I, J from the linear indices L.

matrix<>            A = eye(3,4);
matrix<std::size_t> I, J;
std::tie(I,J) = ind2sub(size(A),find(A));
disp(I);
disp(J);

See nnz, ind2sub.

get

template<typename T>
matrix<T> castor::get(matrix<T> const &A, matrix<std::size_t> const &I, matrix<std::size_t> const &J)
template<typename T>
matrix<T> castor::get(matrix<T> const &A, matrix<std::size_t> const &L)

Get sub-matrix.

get(A,L) return matrix A(L) with elements taken from A corresponding to linear indexing L.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> B = get(A,{0,2,4});
disp(B);

get(A,I,J) return matrix A(I,J) with elements taken from A corresponding to bilinear indexing I for rows and J for columns.

matrix<> A = {{1,2,3},{4,5,6}};
matrix<> B = get(A,{0,1},{1,2});
disp(B);

See set, all, row, col, view, cview.

horzcat

template<typename R, typename S>
inline auto castor::horzcat(R const &A, S const &B)

Horizontal concatenation.

horzcat(A,B) is the horizontal concatenation of matrices A and B. A and B must have the same number of rows. horzcat(A,B) is the same as cat(2,A,B).

matrix<> A = {1,2,3};
matrix<> B = {4,5,6};
matrix<> C = horzcat(A,B);
disp(C);

See vertcat, cat.

ind2sub

template<typename T = std::size_t>
std::tuple<matrix<std::size_t>, matrix<std::size_t>> castor::ind2sub(matrix<std::size_t> S, matrix<T> const &L)

Multiple subscripts from linear index.

ind2sub is used to determine the equivalent subscript values corresponding to a given single index into an array.

(I,J) = ind2sub(S,L) returns the arrays I and J containing the equivalent row and column subscripts corresponding to the index matrix L for a matrix of size S.

matrix<> A = eye(3,4);
matrix<std::size_t> I, J;
std::tie(I,J) = ind2sub(size(A),find(A));
disp(I);
disp(J);

See sub2ind, find.

isempty

template<typename T>
inline bool castor::isempty(matrix<T> const &X)

True for empty array.

isempty(X) returns ‘true’ if X is an empty array and ‘false’ otherwise. An empty array has no elements, that is prod(size(X))==0.

bool a = isempty(zeros(0,0));
bool b = isempty(zeros(2,3));
disp(a);
disp(b);

See isequal, isvector.

isequal

template<typename R, typename S>
bool castor::isequal(matrix<R> const &A, matrix<S> const &B)

True if arrays are numerically equal.

isequal(A,B) returns ‘true’ if arrays A and B are the same size and contain the same values, and ‘false’ otherwise.

bool a = isequal(ones(3,4),ones(3,4));
bool b = isequal(ones(3,4),zeros(3,4));
disp(a);
disp(b);

See isempty, isvector.

isvector

template<typename T>
inline bool castor::isvector(matrix<T> const &X)

True for monodimensional matrix.

isvector(X) returns ‘true’ if X is a monodimensional array and ‘false’ otherwise. A monodimensional array has at least one dimension equal to 1.

bool a = isvector(ones(1,4));
bool b = isvector(ones(3,4));
disp(a);
disp(b);

See isequal, isempty.

range

inline matrix<std::size_t> castor::range(std::size_t j, std::size_t k)

Indices for matrix views.

range(J,K) is simply [J, J+1, …, K[, respecting c++ numbering. This syntax returns an empty matrix if J >= K.

matrix<> A = range(0,5);
disp(A);

See colon, get, set, view.

resize

void castor::matrix::resize(std::size_t m, std::size_t n, T v = (T)NAN)

Resizes and replaces the values of the matrix, eventually filled with the values v specified (by default v is Nan).

matrix<> A = eye(3,2);
A.resize(2,3,0);
disp(A);

See reshape.

reshape

Warning

doxygenfunction: Unable to resolve function “reshape” with arguments (std::size_t, std::size_t) in doxygen xml output for project “castor” from directory: ../xml. Potential matches:

- template<typename T> matrix<T> reshape(matrix<T> const &A, std::size_t m, std::size_t n)
- template<typename T> smatrix<T> reshape(smatrix<T> const &As, std::size_t m, std::size_t n)
- void reshape(std::size_t m, std::size_t n)
- void reshape(std::size_t m, std::size_t n)

See resize, transpose.

row

template<typename T>
matrix<std::size_t> castor::row(matrix<T> const &A)

Bi-linear indexing of row elements.

row(A) return row matrix with row indices of A in bi-linear indexing.

matrix<> A = eye(3,4);
matrix<std::size_t> I = row(A);
matrix<> B = eval(A(I,0));
disp(B);

See all, col, get, set, view, cview.

set

template<typename T, typename U>
void castor::set(matrix<T> &A, matrix<std::size_t> const &I, matrix<std::size_t> const &J, U b)
template<typename T, typename U>
void castor::set(matrix<T> &A, matrix<std::size_t> const &L, matrix<U> const &B)
template<typename T, typename U>
void castor::set(matrix<T> &A, matrix<std::size_t> const &I, matrix<std::size_t> const &J, matrix<U> const &B)
template<typename T, typename U>
void castor::set(matrix<T> &A, matrix<std::size_t> const &L, U b)

Set sub-matrix.

set(A,L,B) modify A with elements taken from B corresponding to linear indexing L, as A(L) = B.

matrix<> A = {{1,2,3},{4,5,6}};
set(A,{0,2,4},-ones(1,3));
disp(A);

set(A,I,J,B) modify A with elements taken from B corresponding to bilinear indexing I for rows and J for columns, as A(I,J) = B.

matrix<> A = {{1,2,3},{4,5,6}};
set(A,{0,1},{1,2},eye(2));
disp(A);

See get, all, row, col, :view, cview.

sub2ind

template<typename T = std::size_t>
matrix<std::size_t> castor::sub2ind(matrix<std::size_t> S, matrix<T> const &I, matrix<T> const &J)

Linear index from multiple subscripts.

sub2ind is used to determine the equivalent single index corresponding to a given set of subscript values.

L = sub2ind(S,I,J) returns the linear index equivalent to the row and column subscripts in the arrays I and J for a matrix of size S.

matrix<> A = eye(3,4);
matrix<std::size_t> I, J, L;
std::tie(I,J) = ind2sub(size(A),find(A));
L = sub2ind(size(A),I,J);
disp(L);

See ind2sub, find.

transpose

template<typename T>
matrix<T> castor::transpose(matrix<T> const &A)

Non-conjugate transpose.

transpose(A) is called for the syntax A^t.

matrix<> A  = {{1,2,3},{4,5,6}};
matrix<> At = transpose(A);
disp(At);

See reshape.

vertcat

template<typename R, typename S>
inline auto castor::vertcat(R const &A, S const &B)

Vertical concatenation.

vertcat(A,B) is the vertical concatenation of array A and B. A and B must have the same number of columns. vertcat(A,B) is the same as cat(1,A,B).

matrix<> A = {1,2,3};
matrix<> B = {4,5,6};
matrix<> C = vertcat(A,B);
disp(C);

See horzcat, cat.