Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Return a read-only view of an input ndarray.

License

NotificationsYou must be signed in to change notification settings

stdlib-js/ndarray-slice

 
 

Repository files navigation

About stdlib...

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.

The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.

When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.

To join us in bringing numerical computing to the web, get started by checking us out onGitHub, and please considerfinancially supporting stdlib. We greatly appreciate your continued support!

slice

NPM versionBuild StatusCoverage Status

Return a read-only view of an inputndarray.

Usage

To use in Observable,

slice=require('https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-slice@umd/browser.js')

To vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:

varslice=require('path/to/vendor/umd/ndarray-slice/index.js')

To include the bundle in a webpage,

<scripttype="text/javascript"src="https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-slice@umd/browser.js"></script>

If no recognized module system is present, access bundle contents via the global scope:

<scripttype="text/javascript">(function(){window.slice;})();</script>

slice( x, ...s[, options] )

Returns aread-only view of an inputndarray.

varSlice=require('@stdlib/slice-ctor');varMultiSlice=require('@stdlib/slice-multi');varndarray=require('@stdlib/ndarray-ctor');varndarray2array=require('@stdlib/ndarray-to-array');varbuffer=[1.0,2.0,3.0,4.0,5.0,6.0];varshape=[3,2];varstrides=[2,1];varoffset=0;varx=ndarray('generic',buffer,shape,strides,offset,'row-major');// returns <ndarray>varsh=x.shape;// returns [ 3, 2 ]vararr=ndarray2array(x);// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]vars0=newSlice(null,null,-2);vars1=newSlice(null,null,-1);vars=newMultiSlice(s0,s1);// returns <MultiSlice>vary=slice(x,s);// returns <ndarray>sh=y.shape;// returns [ 2, 2 ]arr=ndarray2array(y);// returns [ [ 6.0, 5.0 ], [ 2.0, 1.0 ] ]

The function accepts the following arguments:

  • x: inputndarray.
  • s: aMultiSlice instance, an array of slice arguments, or slice arguments as separate arguments.
  • options: function options.

The function supports three (mutually exclusive) means for providing slice arguments:

  1. providing a singleMultiSlice instance.
  2. providing a single array of slice arguments.
  3. providing slice arguments as separate arguments.

The following example demonstrates each invocation style returning equivalent results.

varSlice=require('@stdlib/slice-ctor');varMultiSlice=require('@stdlib/slice-multi');varndarray=require('@stdlib/ndarray-ctor');varndarray2array=require('@stdlib/ndarray-to-array');varbuffer=[1.0,2.0,3.0,4.0,5.0,6.0];varshape=[3,2];varstrides=[2,1];varoffset=0;varx=ndarray('generic',buffer,shape,strides,offset,'row-major');// returns <ndarray>varsh=x.shape;// returns [ 3, 2 ]vararr=ndarray2array(x);// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]// 1. Using a MultiSlice:vars0=newSlice(1,null,1);vars1=newSlice(null,null,1);vars=newMultiSlice(s0,s1);// returns <MultiSlice>vary=slice(x,s);// returns <ndarray>sh=y.shape;// returns [ 2, 2 ]arr=ndarray2array(y);// returns [ [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]// 2. Using an array of slice arguments:y=slice(x,[s0,s1]);// returns <ndarray>sh=y.shape;// returns [ 2, 2 ]arr=ndarray2array(y);// returns [ [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]// 3. Providing separate arguments:y=slice(x,s0,s1);// returns <ndarray>sh=y.shape;// returns [ 2, 2 ]arr=ndarray2array(y);// returns [ [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]

The function supports the followingoptions:

  • strict: boolean indicating whether to enforce strict bounds checking.

By default, the function throws an error when provided a slice which exceeds array bounds. To return an empty array when a slice exceeds array bounds, set thestrict option tofalse.

varSlice=require('@stdlib/slice-ctor');varMultiSlice=require('@stdlib/slice-multi');varndarray=require('@stdlib/ndarray-ctor');varndarray2array=require('@stdlib/ndarray-to-array');varbuffer=[1.0,2.0,3.0,4.0,5.0,6.0];varshape=[3,2];varstrides=[2,1];varoffset=0;varx=ndarray('generic',buffer,shape,strides,offset,'row-major');// returns <ndarray>varsh=x.shape;// returns [ 3, 2 ]vararr=ndarray2array(x);// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]vars0=newSlice(1,null,1);vars1=newSlice(10,20,1);vars=newMultiSlice(s0,s1);// returns <MultiSlice>vary=slice(x,s,{'strict':false});// returns <ndarray>sh=y.shape;// returns [ 2, 0 ]arr=ndarray2array(y);// returns []

Notes

  • Aslice argument must be either aSlice, an integer,null, orundefined.
  • The number of slice dimensions must match the number of array dimensions. Hence, ifx is a zero-dimensionalndarray, then, ifs is aMultiSlice,s should be empty, and, ifs is an array,s should not contain any slice arguments. Similarly, ifx is a one-dimensionalndarray, then, ifs is aMultiSlice,s should have one slice dimension, and, ifs is an array,s should contain a single slice argument. And so on and so forth.

Examples

<!DOCTYPE html><htmllang="en"><body><scripttype="text/javascript"src="https://cdn.jsdelivr.net/gh/stdlib-js/slice-ctor@umd/browser.js"></script><scripttype="text/javascript"src="https://cdn.jsdelivr.net/gh/stdlib-js/slice-multi@umd/browser.js"></script><scripttype="text/javascript"src="https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@umd/browser.js"></script><scripttype="text/javascript"src="https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-to-array@umd/browser.js"></script><scripttype="text/javascript"src="https://cdn.jsdelivr.net/gh/stdlib-js/array-base-zero-to@umd/browser.js"></script><scripttype="text/javascript"src="https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-slice@umd/browser.js"></script><scripttype="text/javascript">(function(){// Alias `null` to allow for more compact indexing expressions:var_=null;// Create a linear ndarray buffer:varbuf=zeroTo(27);// Create an ndarray:varx=array(buf,{'shape':[3,3,3]});// Get each matrix...vars1=E(0,_,_);vary1=slice(x,s1);// returns <ndarray>vara1=ndarray2array(y1);// returns [ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ] ]vars2=E(1,_,_);vary2=slice(x,s2);// returns <ndarray>vara2=ndarray2array(y2);// returns [ [ 9, 10, 11 ], [ 12, 13, 14 ], [ 15, 16, 17 ] ]vars3=E(2,_,_);vary3=slice(x,s3);// returns <ndarray>vara3=ndarray2array(y3);// returns [ [ 18, 19, 20 ], [ 21, 22, 23 ], [ 24, 25, 26 ] ]// Reverse all elements:vars=S(_,_,-1);vars4=E(s,s,s);vary4=slice(x,s4);// returns <ndarray>vara4=ndarray2array(y4);// returns [...]// Get the second rows from each matrix:vars5=E(_,1,_);vary5=slice(x,s5);// returns <ndarray>vara5=ndarray2array(y5);// returns [ [ 3, 4, 5 ], [ 12, 13, 14 ], [ 21, 22, 23 ] ]// Get the second columns from each matrix:vars6=E(_,_,1);vary6=slice(x,s6);// returns <ndarray>vara6=ndarray2array(y6);// returns [ [ 1, 4, 7 ], [ 10, 13, 16 ], [ 19, 22, 25 ] ]// Use an alternative invocation style:vary7=slice(x,_,_,1);// returns <ndarray>vara7=ndarray2array(y7);// returns [ [ 1, 4, 7 ], [ 10, 13, 16 ], [ 19, 22, 25 ] ]// Use an alternative invocation style:vary8=slice(x,[_,_,1]);// returns <ndarray>vara8=ndarray2array(y8);// returns [ [ 1, 4, 7 ], [ 10, 13, 16 ], [ 19, 22, 25 ] ]})();</script></body></html>

See Also


Notice

This package is part ofstdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to developstdlib, see the main projectrepository.

Community

Chat


License

SeeLICENSE.

Copyright

Copyright © 2016-2025. The StdlibAuthors.


[8]ページ先頭

©2009-2025 Movatter.jp