Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

onesLike

Create a ones-filled ndarray having the same shape and data type as a provided ndarray.

Usage

var onesLike = require( '@stdlib/ndarray/base/ones-like' );

onesLike( x )

Creates a ones-filled ndarray having the same shape and data type as a provided ndarray.

var getShape = require( '@stdlib/ndarray/shape' );
var ones = require( '@stdlib/ndarray/base/ones' );

var x = ones( 'float64', [ 2, 2 ], 'row-major' );
// returns <ndarray>[ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]

var y = onesLike( x );
// returns <ndarray>[ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]

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

Notes

  • Along with data type, shape, and order, the function infers the "class" of the returned ndarray from the provided ndarray. For example, if provided a "base" ndarray, the function returns a base ndarray. If provided a non-base ndarray, the function returns a non-base ndarray.

Examples

var dtypes = require( '@stdlib/ndarray/dtypes' );
var ones = require( '@stdlib/ndarray/base/ones' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var onesLike = require( '@stdlib/ndarray/base/ones-like' );

// Get a list of data types:
var dt = dtypes( 'integer_and_generic' );

// Generate ones-filled arrays...
var x;
var i;
for ( i = 0; i < dt.length; i++ ) {
    x = onesLike( ones( dt[ i ], [ 2, 2 ], 'row-major' ) );
    console.log( ndarray2array( x ) );
}