|
| 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 | +# mskput |
| 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 mskput = require( '@stdlib/array/base/mskput' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### mskput( 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 = mskput( x, [ 1, 0, 1, 0 ], [ 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 falsy `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 | +- `'repeat'`: specifies that the function must reuse provided `values` when replacing elements in `x` in order to satisfy the `mask` array. |
| 58 | + |
| 59 | +When `mode` is equal to `'repeat`', the function supports broadcasting a `values` array containing a single element against the number of falsy values in the `mask` array. |
| 60 | + |
| 61 | +```javascript |
| 62 | +var x = [ 1, 2, 3, 4 ]; |
| 63 | + |
| 64 | +var out = mskput( x, [ 1, 0, 1, 0 ], [ 20 ], 'repeat' ); |
| 65 | +// returns [ 1, 20, 3, 20 ] |
| 66 | + |
| 67 | +var bool = ( out === x ); |
| 68 | +// returns true |
| 69 | +``` |
| 70 | + |
| 71 | +</section> |
| 72 | + |
| 73 | +<!-- /.usage --> |
| 74 | + |
| 75 | +<section class="notes"> |
| 76 | + |
| 77 | +## Notes |
| 78 | + |
| 79 | +- The function mutates the input array `x`. |
| 80 | +- If a `mask` array element is falsy, the corresponding element in `x` is **replaced**; otherwise, the corresponding element in `x` is "masked" and thus left unchanged. |
| 81 | + |
| 82 | +</section> |
| 83 | + |
| 84 | +<!-- /.notes --> |
| 85 | + |
| 86 | +<section class="examples"> |
| 87 | + |
| 88 | +## Examples |
| 89 | + |
| 90 | +<!-- eslint no-undef: "error" --> |
| 91 | + |
| 92 | +```javascript |
| 93 | +var filledBy = require( '@stdlib/array/base/filled-by' ); |
| 94 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 95 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 96 | +var linspace = require( '@stdlib/array/base/linspace' ); |
| 97 | +var mskput = require( '@stdlib/array/base/mskput' ); |
| 98 | + |
| 99 | +// Generate a linearly spaced array: |
| 100 | +var x = linspace( 0, 100, 11 ); |
| 101 | +console.log( x ); |
| 102 | + |
| 103 | +// Generate a random mask array: |
| 104 | +var N = discreteUniform( 5, 15 ); |
| 105 | +var mask = filledBy( N, bernoulli.factory( 0.3 ) ); |
| 106 | +console.log( mask ); |
| 107 | + |
| 108 | +// Generate an array of random values: |
| 109 | +var values = filledBy( N, discreteUniform.factory( 1000, 2000 ) ); |
| 110 | +console.log( values ); |
| 111 | + |
| 112 | +// Update a random sample of elements in `x`: |
| 113 | +var out = mskput( x, mask, values, 'throw' ); |
| 114 | +console.log( out ); |
| 115 | +``` |
| 116 | + |
| 117 | +</section> |
| 118 | + |
| 119 | +<!-- /.examples --> |
| 120 | + |
| 121 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 122 | + |
| 123 | +<section class="related"> |
| 124 | + |
| 125 | +</section> |
| 126 | + |
| 127 | +<!-- /.related --> |
| 128 | + |
| 129 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 130 | + |
| 131 | +<section class="links"> |
| 132 | + |
| 133 | +</section> |
| 134 | + |
| 135 | +<!-- /.links --> |
0 commit comments