Search code examples
luatorch

how to extract matrix from big matrix in torch/lua


In torch7 / lua

There is a matrix:

[  1,  2,  3,  4;
   5,  6,  7,  8;
   9, 10, 11, 12;
  13, 14, 15, 16  ]

how to extract this:

[  6,  7;
  10, 11  ]

and how to overwrite it by matrix operation

[  1,  2,  3,  4;
   5, 78, 66,  8;
   9, 45, 21, 12;
  13, 14, 15, 16  ]

Thanks in advance.


Solution

  • matrix
    
    matrix:sub(2,3,2,3)
    
    z = torch.Tensor({{78,66},{45,21}})
    
    matrix:sub(2,3,2,3):copy(z)