In python, we use for i, _ in enumerate(wx): where wx is a row matrix or table.
How can we use this in lua/torch. Any enumerate function?
In Lua, you have pairs and ipairs:
pairs (t)If
thas a metamethod__pairs, calls it withtas argument and returns the first three results from the call.Otherwise, returns three values: the
nextfunction, the tablet, andnil, so that the constructionfor k,v in pairs(t) do body endwill iterate over all key–value pairs of table
t.
You can also use next, for creating your own custom enumeration:
next (table [, index])Allows a program to traverse all fields of a table. Its first argument is a table and its second argument is an index in this table. next returns the next index of the table and its associated value. When called with
nilas its second argument,nextreturns an initial index and its associated value. When called with the last index, or withnilin an empty table,nextreturns nil. If the second argument is absent, then it is interpreted as nil. In particular, you can usenext(t)to check whether a table is empty.The order in which the indices are enumerated is not specified, even for numeric indices. (To traverse a table in numerical order, use a numerical for.)
The behavior of
nextis undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may clear existing fields.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With