Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
Take elements from either one of two arrays depending on a condition.
License
stdlib-js/array-base-where
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!
Take elements from either one of two arrays depending on a condition.
npm install @stdlib/array-base-where
Alternatively,
- To load the package in a website via a
scripttag without installation and bundlers, use theES Module available on theesmbranch (seeREADME). - If you are using Deno, visit the
denobranch (seeREADME for usage intructions). - For use in Observable, or in browser/node environments, use theUniversal Module Definition (UMD) build available on the
umdbranch (seeREADME).
Thebranches.md file summarizes the available branches and displays a diagram illustrating their relationships.
To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
varwhere=require('@stdlib/array-base-where');
Takes elements from eitherx ory depending on a condition.
varx=[1,2,3,4];vary=[5,6,7,8];varcondition=[true,false,true,false];varz=where(condition,x,y);// returns [ 1, 6, 3, 8 ]
The function supports the following parameters:
- condition: array of values indicating whether to take an element from either
xory. If a condition element is truthy, the function takes a respective element fromx; otherwise, the function takes a respective element fromy. If non-empty, must be broadcastcompatible with the resolved output array length. - x: first input array. If
conditionis non-empty, must be broadcastcompatible with the resolved output array length. - y: second input array. If
conditionis non-empty, must be broadcastcompatible with the resolved output array length.
When all input arrays are non-empty, the function supports broadcasting single-element arrays to the resolved output array length, which is equal to the maximum length of all provided input arrays.
varx=[1,2,3,4];vary=[5];varcondition=[true,false,true,false];varz=where(condition,x,y);// returns [ 1, 5, 3, 5 ]z=where(condition,y,x);// returns [ 5, 2, 5, 4 ]z=where([true],x,y);// returns [ 1, 2, 3, 4 ]z=where([false],x,y);// returns [ 5, 5, 5, 5 ]z=where(condition,[1],y);// returns [ 1, 5, 1, 5 ]
Ifcondition is an empty array, the function returns an empty array.
varx=[1,2,3,4];vary=[5,6,7,8];varcondition=[];varz=where(condition,x,y);// returns []
Takes elements from eitherx ory depending on a condition and assigns the values to elements in a provided output array.
varx=[1,2,3,4];vary=[5,6,7,8];varout=[0,0,0,0];varcondition=[true,false,true,false];vararr=where.assign(condition,x,y,out,1,0);// returns [ 1, 6, 3, 8 ]varbool=(arr===out);// returns true
The function supports the following parameters:
- condition: array of values indicating whether to take an element from either
xory. If a condition element is truthy, the function takes a respective element fromx; otherwise, the function takes a respective element fromy. If non-empty, must be broadcastcompatible with the output array. - x: first input array. If
conditionis non-empty, must be broadcastcompatible with the output array. - y: second input array. If
conditionis non-empty, must be broadcastcompatible with the output array. - out: output array.
- stride: output array stride.
- offset: output array offset.
The function supports broadcasting single-element arrays to the output array length.
varx=[1,2,3,4];vary=[5];varcondition=[true,false,true,false];varout=[0,0,0,0];vararr=where.assign(condition,x,y,out,1,0);// returns [ 1, 5, 3, 5 ]out=[0,0,0,0];arr=where.assign(condition,y,x,out,1,0);// returns [ 5, 2, 5, 4 ]out=[0,0,0,0];arr=where.assign([true],x,y,out,1,0);// returns [ 1, 2, 3, 4 ]out=[0,0,0,0];arr=where.assign([false],x,y,out,1,0);// returns [ 5, 5, 5, 5 ]out=[0,0,0,0];arr=where.assign(condition,[1],y,out,1,0);// returns [ 1, 5, 1, 5 ]
Whencondition is an empty array, the function returns the output array unchanged.
varx=[1,2,3,4];vary=[5,6,7,8];varout=[0,0,0,0];varcondition=[];vararr=where.assign(condition,x,y,out,1,0);// returns [ 0, 0, 0, 0 ]varbool=(arr===out);// returns true
vardiscreteUniform=require('@stdlib/random-array-discrete-uniform');varbernoulli=require('@stdlib/random-array-bernoulli');varwhere=require('@stdlib/array-base-where');varopts={'dtype':'generic'};// Generate an array of indicator values:varcondition=bernoulli(20,0.9,opts);console.log(condition);// Generate an array of random values:varx=discreteUniform(condition.length,0,10,opts);console.log(x);// Define an array containing a broadcasted "missing" value:vary=[NaN];// Return an array with randomly placed missing values:varz=where(condition,x,y);console.log(z);
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
Take elements from either one of two arrays depending on a condition.
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.