Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

truesLike

Create an ndarray filled with true values and having the same shape and data type as a provided ndarray.

Usage

var truesLike = require( '@stdlib/ndarray/trues-like' );

truesLike( x[, options] )

Creates an ndarray filled with true values and having the same shape and data type as a provided ndarray.

var getShape = require( '@stdlib/ndarray/shape' );
var getDType = require( '@stdlib/ndarray/dtype' );
var empty = require( '@stdlib/ndarray/empty' );

var x = empty( [ 2, 2 ], {
    'dtype': 'bool'
});
// returns <ndarray>

var y = truesLike( x );
// returns <ndarray>[ [ true, true ], [ true, true ] ]

var sh = getShape( y );
// returns [ 2, 2 ]

var dt = String( getDType( y ) );
// returns 'bool'

The function supports the following options:

  • dtype: output ndarray data type. Must be a boolean or "generic" data type. Overrides the input ndarray's inferred data type.
  • shape: output ndarray shape. Overrides the input ndarray's inferred shape.
  • order: specifies whether the output ndarray should be 'row-major' (C-style) or 'column-major' (Fortran-style). Overrides the input ndarray's inferred order.
  • mode: specifies how to handle indices which exceed array dimensions (see ndarray). Default: 'throw'.
  • submode: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions (see ndarray). If provided fewer modes than dimensions, the constructor recycles modes using modulo arithmetic. Default: [ options.mode ].
  • readonly: boolean indicating whether an array should be read-only. Default: false.

To override either the dtype, shape, or order, specify the corresponding option. For example, to override the inferred data type,

var getShape = require( '@stdlib/ndarray/shape' );
var getDType = require( '@stdlib/ndarray/dtype' );
var empty = require( '@stdlib/ndarray/empty' );

var x = empty( [ 2, 2 ], {
    'dtype': 'bool'
});
// returns <ndarray>

var dt = String( getDType( x ) );
// returns 'bool'

var y = truesLike( x, {
    'dtype': 'generic'
});
// returns <ndarray>[ [ true, true ], [ true, true ] ]

var sh = getShape( y );
// returns [ 2, 2 ]

dt = String( getDType( y ) );
// returns 'generic'

Examples

var empty = require( '@stdlib/ndarray/empty' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var truesLike = require( '@stdlib/ndarray/trues-like' );

// Specify a list of data types:
var dt = [
    'bool',
    'generic'
];

// Generate filled ndarrays...
var x;
var y;
var i;
for ( i = 0; i < dt.length; i++ ) {
    x = empty( [ 2, 2 ], {
        'dtype': dt[ i ]
    });
    y = truesLike( x );
    console.log( ndarray2array( y ) );
}

See Also