Torch / Lua, how to select a subset of an array or tensor? -


i'm working on torch/lua , have array dataset of 10 elements.

dataset = {11,12,13,14,15,16,17,18,19,20} 

if write dataset[1], can read structure of 1st element of array.

th> dataset[1] 11   

i need select 3 elements among 10, don't know command use. if working on matlab, write: dataset[1:3], here not work.

do have suggestions?

in torch

th> x = torch.tensor{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 

to select range, first three, use the index operator:

th> x[{{1,3}}] 1 2 3 

where 1 'start' index, , 3 'end' index.

see extracting sub-tensors more alternatives using tensor.sub , tensor.narrow


in lua 5.2 or less

lua tables, such dataset variable, not have method selecting sub-ranges.

function subrange(t, first, last)   local sub = {}   i=first,last     sub[#sub + 1] = t[i]   end   return sub end  dataset = {11,12,13,14,15,16,17,18,19,20}  sub = subrange(dataset, 1, 3) print(unpack(sub)) 

which prints

11    12   13 

in lua 5.3

in lua 5.3 can use table.move.

function subrange(t, first, last)      return table.move(t, first, last, 1, {}) end 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -