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

Take elements from either one of two arrays depending on a condition.

License

NotificationsYou must be signed in to change notification settings

stdlib-js/array-base-where

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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!

where

NPM versionBuild StatusCoverage Status

Take elements from either one of two arrays depending on a condition.

Usage

importwherefrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-base-where@deno/mod.js';

You can also import the following named exports from the package:

import{assign}from'https://cdn.jsdelivr.net/gh/stdlib-js/array-base-where@deno/mod.js';

where( condition, x, y )

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 eitherx ory. 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. Ifcondition is non-empty, must be broadcastcompatible with the resolved output array length.
  • y: second input array. Ifcondition is 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 []

where.assign( condition, x, y, out, stride, offset )

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 eitherx ory. 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. Ifcondition is non-empty, must be broadcastcompatible with the output array.
  • y: second input array. Ifcondition is 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

Examples

importdiscreteUniformfrom'https://cdn.jsdelivr.net/gh/stdlib-js/random-array-discrete-uniform@deno/mod.js';importbernoullifrom'https://cdn.jsdelivr.net/gh/stdlib-js/random-array-bernoulli@deno/mod.js';importwherefrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-base-where@deno/mod.js';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);

Notice

This package is part ofstdlib, a standard library 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.

About

Take elements from either one of two arrays depending on a condition.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp