|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# place |
| 22 | + |
| 23 | +> Replace elements of an array with provided values according to a provided mask array. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var place = require( '@stdlib/array/base/place' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### place( x, mask, values, mode ) |
| 34 | + |
| 35 | +Replaces elements of an array with provided values according to a provided mask array. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var x = [ 1, 2, 3, 4 ]; |
| 39 | + |
| 40 | +var out = place( x, [ 0, 1, 0, 1 ], [ 20, 40 ], 'throw' ); |
| 41 | +// returns [ 1, 20, 3, 40 ] |
| 42 | + |
| 43 | +var bool = ( out === x ); |
| 44 | +// returns true |
| 45 | +``` |
| 46 | + |
| 47 | +The function supports the following parameters: |
| 48 | + |
| 49 | +- **x**: input array. |
| 50 | +- **mask**: mask array. |
| 51 | +- **values**: values to set. |
| 52 | +- **mode**: string specifying whether to raise an exception when the number of `values` is less than the number of truthy `mask` values. |
| 53 | + |
| 54 | +The function supports the following modes: |
| 55 | + |
| 56 | +- `'throw'`: specifies that the function must raise an exception when the function is provided insufficient `values` to satisfy the `mask` array. |
| 57 | +- `'broadcast'`: specifies that the function must broadcast a single-element `values` array and otherwise raise an exception when the function is provided insufficient `values` to satisfy the `mask` array. |
| 58 | +- `'repeat'`: specifies that the function must reuse provided `values` when replacing elements in `x` in order to satisfy the `mask` array. |
| 59 | + |
| 60 | +When `mode` is equal to `'broadcast`', the function supports broadcasting a `values` array containing a single element against the number of truthy values in the `mask` array. |
| 61 | + |
| 62 | +```javascript |
| 63 | +var x = [ 1, 2, 3, 4 ]; |
| 64 | + |
| 65 | +var out = place( x, [ 0, 1, 0, 1 ], [ 20 ], 'broadcast' ); |
| 66 | +// returns [ 1, 20, 3, 20 ] |
| 67 | + |
| 68 | +var bool = ( out === x ); |
| 69 | +// returns true |
| 70 | +``` |
| 71 | + |
| 72 | +When `mode` is equal to `repeat`, the function supports recycling elements in a `values` array to satisfy the number of truthy values in the `mask` array. |
| 73 | + |
| 74 | +```javascript |
| 75 | +var x = [ 1, 2, 3, 4 ]; |
| 76 | + |
| 77 | +var out = place( x, [ 1, 1, 0, 1 ], [ 20, 40 ], 'repeat' ); |
| 78 | +// returns [ 20, 40, 3, 20 ] |
| 79 | + |
| 80 | +var bool = ( out === x ); |
| 81 | +// returns true |
| 82 | +``` |
| 83 | + |
| 84 | +</section> |
| 85 | + |
| 86 | +<!-- /.usage --> |
| 87 | + |
| 88 | +<section class="notes"> |
| 89 | + |
| 90 | +## Notes |
| 91 | + |
| 92 | +- The function mutates the input array `x`. |
| 93 | +- If a `mask` array element is truthy, the corresponding element in `x` is **replaced**; otherwise, the corresponding element in `x` is "masked" and thus left unchanged. |
| 94 | + |
| 95 | +</section> |
| 96 | + |
| 97 | +<!-- /.notes --> |
| 98 | + |
| 99 | +<section class="examples"> |
| 100 | + |
| 101 | +## Examples |
| 102 | + |
| 103 | +<!-- eslint no-undef: "error" --> |
| 104 | + |
| 105 | +```javascript |
| 106 | +var filledBy = require( '@stdlib/array/base/filled-by' ); |
| 107 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 108 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 109 | +var linspace = require( '@stdlib/array/base/linspace' ); |
| 110 | +var place = require( '@stdlib/array/base/place' ); |
| 111 | + |
| 112 | +// Generate a linearly spaced array: |
| 113 | +var x = linspace( 0, 100, 11 ); |
| 114 | +console.log( x ); |
| 115 | + |
| 116 | +// Generate a random mask array: |
| 117 | +var N = discreteUniform( 5, 15 ); |
| 118 | +var mask = filledBy( N, bernoulli.factory( 0.3 ) ); |
| 119 | +console.log( mask ); |
| 120 | + |
| 121 | +// Generate an array of random values: |
| 122 | +var values = filledBy( N, discreteUniform.factory( 1000, 2000 ) ); |
| 123 | +console.log( values ); |
| 124 | + |
| 125 | +// Update a random sample of elements in `x`: |
| 126 | +var out = place( x, mask, values, 'throw' ); |
| 127 | +console.log( out ); |
| 128 | +``` |
| 129 | + |
| 130 | +</section> |
| 131 | + |
| 132 | +<!-- /.examples --> |
| 133 | + |
| 134 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 135 | + |
| 136 | +<section class="related"> |
| 137 | + |
| 138 | +</section> |
| 139 | + |
| 140 | +<!-- /.related --> |
| 141 | + |
| 142 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 143 | + |
| 144 | +<section class="links"> |
| 145 | + |
| 146 | +</section> |
| 147 | + |
| 148 | +<!-- /.links --> |
0 commit comments