Matrix into a 4-tensor

137 Views Asked by At

I've been trying to figure out a way to add a dimension to a matrix with a certain rule:

Let our matrix X have dimensions NxM, L be some positive number, and the desired resulting 4-tensor (3d matrix) to be called R.

R should have dimensions LxMxN and be constructed like that: the LxM matrix with third axis coordinate i should be the i-th row of X repeated L times vertically.

Put shortly, if X*A = R, what is A?

Example: If X = [[1 2], [3 0], [2 4]] and L = 4

We want the result to be: R =
[[[1 2], [1 2], [1 2], [1 2]],
[[3 0], [3 0], [3 0], [3 0]],
[[2 4], [2 4], [2 4], [2 4]]]

My final goal is to use it as part of a Matlab program where I should use only preloaded matrix functions and not loops for optimization purposes.