Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
Return a read-only view of an input ndarray.
License
stdlib-js/ndarray-slice
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
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!
Return a read-only view of an input
ndarray.
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>
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: input
ndarray. - s: a
MultiSliceinstance, 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:
- providing a single
MultiSliceinstance. - providing a single array of slice arguments.
- 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 []
- Aslice argument must be either a
Slice, an integer,null, orundefined. - The number of slice dimensions must match the number of array dimensions. Hence, if
xis a zero-dimensionalndarray, then, ifsis aMultiSlice,sshould be empty, and, ifsis an array,sshould not contain any slice arguments. Similarly, ifxis a one-dimensionalndarray, then, ifsis aMultiSlice,sshould have one slice dimension, and, ifsis an array,sshould contain a single slice argument. And so on and so forth.
<!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>
@stdlib/ndarray-array:multidimensional arrays.@stdlib/ndarray-at:return an ndarray element.@stdlib/ndarray-ctor:multidimensional array constructor.@stdlib/ndarray-slice-assign:assign element values from a broadcasted input ndarray to corresponding elements in an output ndarray view.@stdlib/ndarray-slice-dimension:return a read-only view of an input ndarray when sliced along a specified dimension.
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.
SeeLICENSE.
Copyright © 2016-2025. The StdlibAuthors.
About
Return a read-only view of an input ndarray.
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.