Created
December 3, 2019 18:07
-
-
Save khafatech/b496f4f6ed10e3decc703b69a826d22c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( | |
var numRows = 3; | |
var numCols = 4; | |
// a = Array.geom(numRows*numCols,1,3); | |
a = Array.series(numRows*numCols, start: 1, step: 1); | |
/* | |
[ [ 1, 2, 3, 4, 5 ], | |
[ 6, 7, 8, 9, 10 ], | |
[ 11, 12, 13, 14, 15 ]] | |
*/ | |
// 3 rows, 4 cols | |
b = Array2D.fromArray(numRows, numCols, a); | |
// throws error | |
// b[0]; | |
b[0,0]; // 1st element | |
b[1,2]; // 2nd row, 3rd col | |
b[1,5]; // 10th element, not error | |
// or, create an Array of Array's | |
// c is not an Array2D | |
c = a.reshape(numRows, numCols); | |
c[0]; // first row - an array | |
c[0][0]; // 1st element | |
c[1][2]; // 2nd row, 3rd col | |
// the comma syntax doesn't work; returns | |
c[1,5]; // 2nd row | |
// over the column limit nil | |
c[1][5]; | |
// over the row limit. | |
// c[100] returns nil | |
// The 2nd subscript, [5], tries to subscript nil and throws error | |
// c[100][5]; | |
// last element | |
c[numRows-1][numCols-1]; | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment