NdArray::ndim

Number of dimensions of an array.

Description

int NdArray::ndim

Return Values

A positive integer.

Examples

Example #1: Get the number of dimensions

use SciPhp\NumPhp as np;

$m = np::linspace(1, 9, 9)->reshape(3, 3);

$dim = $m->ndim;

echo 
"m\n$m", "m->ndim=$dim\n";

The above example will output:

m
[[1  2  3]
 [4  5  6]
 [7  8  9]]
m->ndim=2

Example #2: Changing the number of dimensions

use SciPhp\NumPhp as np;

$m = np::ar([1, 2, 3]);

// Transforming a line vector into a column vector
$n = $m->resize(3, 1);

echo 
"m (ndim={$m->ndim})\n{$m}", "\nn (ndim={$n->ndim})\n{$n}";

The above example will output:

m (ndim=1)
[1  2  3]

n (ndim=2)
[[1]
 [2]
 [3]]

See Also