README.md (4505B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # ind2sub 22 23 > Convert a linear index to an array of subscripts. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var ind2sub = require( '@stdlib/ndarray/ind2sub' ); 41 ``` 42 43 #### ind2sub( shape, idx\[, options] ) 44 45 Converts a linear index to an array of subscripts. 46 47 ```javascript 48 var shape = [ 2, 2 ]; 49 50 var s = ind2sub( shape, 2 ); 51 // returns [ 1, 0 ] 52 ``` 53 54 The function supports the following `options`: 55 56 - `mode`: specifies how to handle a linear index which exceeds array dimensions. The following modes are supported: 57 58 - `throw`: specifies that the function should throw an error when a linear index exceeds array dimensions. 59 - `wrap`: specifies that the function should wrap around a linear index exceeding array dimensions using modulo arithmetic. 60 - `clamp`: specifies that the function should set a linear index exceeding array dimensions to either `0` (minimum linear index) or the maximum linear index. 61 62 - `order`: specifies whether an array is `row-major` (C-style) or `column-major` (Fortran-style). Default: `'row-major'`. 63 64 By default, the function assumes a row-major array. To return subscripts for a column-major array, set the `order` option. 65 66 ```javascript 67 var shape = [ 2, 2 ]; 68 var opts = {}; 69 70 opts.order = 'column-major'; 71 var s = ind2sub( shape, 1, opts ); 72 // returns [ 1, 0 ] 73 ``` 74 75 By default, the function throws an `error` if provided a linear index which exceeds array dimensions. To specify alternative behavior, set the `mode` option. 76 77 ```javascript 78 var shape = [ 2, 2 ]; 79 var opts = {}; 80 81 opts.mode = 'wrap'; 82 var s = ind2sub( shape, -5, opts ); 83 // returns [ 1, 1 ] 84 85 opts.mode = 'clamp'; 86 s = ind2sub( shape, 7, opts ); 87 // returns [ 1, 1 ] 88 ``` 89 90 #### ind2sub.assign( shape, idx\[, options], out ) 91 92 Converts a linear index to an array of subscripts and assigns results to a provided output array. 93 94 ```javascript 95 var shape = [ 2, 2 ]; 96 var out = [ 0, 0 ]; 97 98 var subscripts = ind2sub.assign( shape, 1, out ); 99 // returns [ 0, 1 ] 100 101 var bool = ( subscripts === out ); 102 // returns true 103 ``` 104 105 The function accepts the same `options` as above. 106 107 </section> 108 109 <!-- /.usage --> 110 111 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 112 113 <section class="notes"> 114 115 </section> 116 117 <!-- /.notes --> 118 119 <!-- Package usage examples. --> 120 121 <section class="examples"> 122 123 ## Examples 124 125 <!-- eslint no-undef: "error" --> 126 127 ```javascript 128 var numel = require( '@stdlib/ndarray/base/numel' ); 129 var ind2sub = require( '@stdlib/ndarray/ind2sub' ); 130 131 var shape = [ 3, 3, 3 ]; 132 var len = numel( shape ); 133 134 var arr = []; 135 var i; 136 for ( i = 0; i < len; i++ ) { 137 arr.push( i ); 138 } 139 140 var opts = { 141 'order': 'column-major' 142 }; 143 144 console.log( '' ); 145 console.log( 'Dimensions: %s.', shape.join( 'x' ) ); 146 console.log( 'View (subscripts):' ); 147 console.log( '' ); 148 149 var row; 150 var s; 151 152 row = ' '; 153 for ( i = 0; i < len; i++ ) { 154 s = ind2sub( shape, i, opts ); 155 row += '(' + s.join( ',' ) + ')'; 156 if ( (i+1)%shape[0] === 0 ) { 157 console.log( row ); 158 row = ' '; 159 } else { 160 row += ', '; 161 } 162 if ( (i+1)%(shape[0]*shape[1]) === 0 ) { 163 console.log( '' ); 164 } 165 } 166 ``` 167 168 </section> 169 170 <!-- /.examples --> 171 172 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 173 174 <section class="references"> 175 176 </section> 177 178 <!-- /.references --> 179 180 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 181 182 <section class="links"> 183 184 </section> 185 186 <!-- /.links -->