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.
To use in Observable,
where=require('https://cdn.jsdelivr.net/gh/stdlib-js/array-base-where@umd/browser.js')
To vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:
varwhere=require('path/to/vendor/umd/array-base-where/index.js')
To include the bundle in a webpage,
<scripttype="text/javascript"src="https://cdn.jsdelivr.net/gh/stdlib-js/array-base-where@umd/browser.js"></script>
If no recognized module system is present, access bundle contents via the global scope:
<scripttype="text/javascript">(function(){window.where;})();</script>
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
<!DOCTYPE html><htmllang="en"><body><scripttype="text/javascript"src="https://cdn.jsdelivr.net/gh/stdlib-js/random-array-discrete-uniform@umd/browser.js"></script><scripttype="text/javascript"src="https://cdn.jsdelivr.net/gh/stdlib-js/random-array-bernoulli@umd/browser.js"></script><scripttype="text/javascript"src="https://cdn.jsdelivr.net/gh/stdlib-js/array-base-where@umd/browser.js"></script><scripttype="text/javascript">(function(){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);})();</script></body></html>
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.