Created
January 9, 2012 12:51
-
-
Save lsauer/1582813 to your computer and use it in GitHub Desktop.
Convert a Vector into a Matrix | Linear List into a Table | Expand Vector, Flatten Matrix
This file contains 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
//lo sauer | |
//fn:Array1dTo2d 1D array to 2D array | |
//@param a {Array}: list of elements | |
//@param len {Int}: number of elements in each row | |
Array1dTo2d = function(a, len){ | |
var b=[]; | |
for(var i=0; i<a.length; i+=len) | |
b.push( a.slice(i,i+len) ); | |
return b; | |
} | |
Array1dTo2d([1,2,3,4,5,6,7,8,9,10,11,12],3); | |
//> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] | |
//fn:Matrix: converts linear list of list elements into an nxm matrix | |
//@param alist {Array}: linear list of elements | |
//@param n {Int}: #rows of the matrix | |
//@param m {Int}: #rows of the matrix optional:true; else n=m | |
function Matrix(alist,n,m) { | |
m||(m = n); | |
var mat = [] | |
for(var i=0,len=n*m;i<len;i+=m) | |
mat.push(alist.slice(i,i+m)) | |
return mat; | |
} | |
Matrix([1,2,3,4,5,6,7,8,9,10,11,12],3); | |
//> [[1, 2, 3], [4, 5, 6], [7, 8, 9]] | |
Matrix([1,2,3,4,5,6,7,8,9,10,11,12],3,4); | |
//> [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] | |
Matrix([1,2,3,4,5,6,7,8,9,10,11,12],4,3); | |
//> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] | |
//fn:List | |
//reverse operation to 'Matrix': flattens an array | |
//@param mat {Array}: | |
function List(mat){ | |
return (mat+'').split(',').map(parseFloat); | |
} | |
List(Matrix([1,2,3,4,5,6,7,8,9,10,11,12],3,4)); | |
//> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] |
This file contains 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
### lo sauer, 2011 | |
fn:Array1dTo2d 1D array to 2D array | |
@param a {Array}: list of elements | |
@param len {Int}: number of elements in each row | |
### | |
Array1dTo2d = (a, len) -> | |
b = [] | |
i = 0 | |
while i < a.length | |
b.push a.slice(i, i + len) | |
i += len | |
b | |
Array1dTo2d [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ], 3 | |
#> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] | |
Matrix = (alist, n, m) -> | |
m or (m = n) | |
mat = [] | |
i = 0 | |
len = n * m | |
while i < len | |
mat.push alist.slice(i, i + m) | |
i += m | |
mat | |
Matrix [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ], 3 | |
Matrix [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ], 3, 4 | |
Matrix [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ], 4, 3 | |
#> [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ] | |
List = (mat) -> | |
(mat + "").split(",").map parseFloat | |
List Matrix([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ], 3, 4) | |
#> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment