Matrix dimensions

These functions allow to recover the dimensions of a matrix : total number of elements (see numel), dimensions (see size), etc.

length

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

Length of vector.

length(V) returns the length of vector V. For matrix A, it is equivalent to max(size(A)).

matrix<> A = ones(1,3);
matrix<> B = ones(4,3);
disp(length(A));
disp(length(B));

See numel, size.

nnz

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

Number of nonzero matrix elements.

nz = nnz(A) is the number of nonzero elements in A.

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

See find, size.

numel

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

Number of elements in a array.

N = numel(A) returns the number of elements, N, in array A, equivalent to prod(size(A)).

matrix<>    A = ones(3,4);
std::size_t n = numel(A);
disp(n);

See length, size.

size

template<typename T>
std::size_t castor::size(matrix<T> const &A, int dim)
template<typename T>
matrix<std::size_t> castor::size(matrix<T> const &A)

Size of array.

S = size(A) for m-by-n matrix A returns the two-element vector [m,n] containing the number of rows and columns in the matrix.

S = size(A,dim) returns the lengths of the specified dimensions dim.

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

See length, numel.