README.md (4026B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2020 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 # mediansorted 22 23 > Calculate the median value of a sorted strided array. 24 25 <section class="intro"> 26 27 </section> 28 29 <!-- /.intro --> 30 31 <section class="usage"> 32 33 ## Usage 34 35 ```javascript 36 var mediansorted = require( '@stdlib/stats/base/mediansorted' ); 37 ``` 38 39 #### mediansorted( N, x, stride ) 40 41 Computes the median value of a sorted strided array `x`. 42 43 ```javascript 44 var x = [ 1.0, 2.0, 3.0 ]; 45 var v = mediansorted( x.length, x, 1 ); 46 // returns 2.0 47 48 x = [ 3.0, 2.0, 1.0 ]; 49 v = mediansorted( x.length, x, 1 ); 50 // returns 2.0 51 ``` 52 53 The function has the following parameters: 54 55 - **N**: number of indexed elements. 56 - **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. 57 - **stride**: index increment for `x`. 58 59 The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the median value of every other element in `x`, 60 61 ```javascript 62 var floor = require( '@stdlib/math/base/special/floor' ); 63 64 var x = [ 1.0, 2.0, 2.0, -7.0, 3.0, 3.0, 4.0, 2.0 ]; 65 var N = floor( x.length / 2 ); 66 67 var v = mediansorted( N, x, 2 ); 68 // returns 2.5 69 ``` 70 71 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 72 73 <!-- eslint-disable stdlib/capitalized-comments --> 74 75 ```javascript 76 var Float64Array = require( '@stdlib/array/float64' ); 77 var floor = require( '@stdlib/math/base/special/floor' ); 78 79 var x0 = new Float64Array( [ 2.0, 1.0, 2.0, 2.0, -2.0, 2.0, 3.0, 4.0 ] ); 80 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 81 82 var N = floor( x0.length / 2 ); 83 84 var v = mediansorted( N, x1, 2 ); 85 // returns 2.0 86 ``` 87 88 #### mediansorted.ndarray( N, x, stride, offset ) 89 90 Computes the median value of a sorted strided array using alternative indexing semantics. 91 92 ```javascript 93 var x = [ 1.0, 2.0, 3.0 ]; 94 95 var v = mediansorted.ndarray( x.length, x, 1, 0 ); 96 // returns 2.0 97 ``` 98 99 The function has the following additional parameters: 100 101 - **offset**: starting index for `x`. 102 103 While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the median value for every other value in `x` starting from the second value 104 105 ```javascript 106 var floor = require( '@stdlib/math/base/special/floor' ); 107 108 var x = [ 2.0, 1.0, 2.0, 2.0, -2.0, 2.0, 3.0, 4.0 ]; 109 var N = floor( x.length / 2 ); 110 111 var v = mediansorted.ndarray( N, x, 2, 1 ); 112 // returns 2.0 113 ``` 114 115 </section> 116 117 <!-- /.usage --> 118 119 <section class="notes"> 120 121 ## Notes 122 123 - If `N <= 0`, both functions return `NaN`. 124 - The input strided array must be sorted in either **strictly** ascending or descending order. 125 126 </section> 127 128 <!-- /.notes --> 129 130 <section class="examples"> 131 132 ## Examples 133 134 <!-- eslint no-undef: "error" --> 135 136 ```javascript 137 var Float64Array = require( '@stdlib/array/float64' ); 138 var mediansorted = require( '@stdlib/stats/base/mediansorted' ); 139 140 var x; 141 var i; 142 143 x = new Float64Array( 10 ); 144 for ( i = 0; i < x.length; i++ ) { 145 x[ i ] = i - 5.0; 146 } 147 console.log( x ); 148 149 var v = mediansorted( x.length, x, 1 ); 150 console.log( v ); 151 ``` 152 153 </section> 154 155 <!-- /.examples --> 156 157 <section class="links"> 158 159 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 160 161 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 162 163 </section> 164 165 <!-- /.links -->