Search code examples
arraysmatlabdimension

How do I recursively add dimensions to MATLAB array?


I want to create a loop that grows a given matrix in dimension a set number of times in the following fashion:

a = zeros(5,2) 
a = cat(3,a,zeros(5,2))
a = cat(4,a,zeros(5,2,2))
a = cat(5,a,zeros(5,2,2,2))
a = cat(6,a,zeros(5,2,2,2,2))

Solution

  • a = zeros(5, 2)
    for i = 3:6
      a = cat(i, a, zeros(size(a)));
    end
    

    This only holds, if the assignment specifically takes place as mentioned in the question. If there are any other constraints, please specify.