Search code examples
octave

Is there a way to store multiple matrices in a vector in octave?


I am quite new to octave and have the following question: In Mathematica, you can initialize a vector, say t = {0,0,0} and can use each entry of t to store a matrix of possibly different size. For instance you could set

t[[1]] = IdentityMatrix[3];
t[[2]] = IdentityMatrix[4];
t[[3]] = IdentityMatrix[5];

giving you a 'tensor' with different dimensions for different first indices. Is there any equivalent way in Octave? My naive attempts failed. I would like to use it in a neural network, where I need to reshape a long vector into several matrices depending on the number and size of the layers, which I would like to store in a vector for convenience of scalability.

Is this or something similar possible?

Thanks in advance!


Solution

  • This is what cell arrays are for.

    E.g.

    t = { eye(3), eye(4), eye(5) }
    

    gives

    t =
    {
      [1,1] =
    Diagonal Matrix
         1   0   0
         0   1   0
         0   0   1
      [1,2] =
    Diagonal Matrix
         1   0   0   0
         0   1   0   0
         0   0   1   0
         0   0   0   1
      [1,3] =
    Diagonal Matrix
         1   0   0   0   0
         0   1   0   0   0
         0   0   1   0   0
         0   0   0   1   0
         0   0   0   0   1
    }