diff options
author | Determinant <[email protected]> | 2015-06-21 13:51:15 +0800 |
---|---|---|
committer | Determinant <[email protected]> | 2015-06-21 13:51:15 +0800 |
commit | 9a8ef8091806c004bc53c529d0b9f514eb6ac3da (patch) | |
tree | 0f228f33301419618417efb1fb2751be87e0a878 /matrix | |
parent | 9ced612c86b5e24409e8f1d81bfbd1da893f2157 (diff) |
add nerv.Matrix:generate()
Diffstat (limited to 'matrix')
-rw-r--r-- | matrix/init.lua | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/matrix/init.lua b/matrix/init.lua index 769ef2d..1a8925f 100644 --- a/matrix/init.lua +++ b/matrix/init.lua @@ -27,17 +27,18 @@ function nerv.Matrix:__tostring__() return table.concat(strt) end ---func: function ---All entrys in the matrix will be assigned by calling func() -function nerv.Matrix:randomize(func) +-- gen: a function takes take indices of the matrix and return the generated +-- all entrys in the matrix will be assigned by calling gen(i, j) +function nerv.Matrix:generate(gen) if (self:nrow() == 1) then - for j = 0, self:ncol() - 1, 1 do - self[j] = func() + for j = 0, self:ncol() - 1 do + self[j] = gen(j) end else - for i = 0, self:nrow() - 1, 1 do - for j = 0, self:ncol() - 1, 1 do - self[i][j] = func() + for i = 0, self:nrow() - 1 do + local row = self[i] + for j = 0, self:ncol() - 1 do + row[j] = gen(i, j) end end end |