Array within an array in MATLAB
1 answer
You will need a cell array to store your numeric vectors . Array cells are used in Matlab when the contents of each cell are of a different size or type.
Additional comments:
- I renamed the variable
i
tok
to avoid shading the imaginary unit. - I also renamed the variable
table
tot
to avoid function shadingtable
. -
zeros(k)
gives a matrix of zerosk
xk
. To get a vector of strings of zeros usezeros(1,k)
. - Better to preallocate the cell array to improve speed.
With that said, the code:
n = 4; t = cell(1,n); %// preallocate: 1xn cell array of empty cells for k = 1:n t{k} = zeros(1,k); end
This gives:
>> celldisp(t)
t{1} =
0
t{2} =
0 0
t{3} =
0 0 0
t{4} =
0 0 0 0
Equivalently, you could replace the loop with a for
more compact one arrayfun
:
result = arrayfun(@(k) zeros(1,k), 1:n, 'uniformoutput', false);
+5
source to share