MATLAB: Loop that creates arrays

arrayloop

Hey folks I'm trying to write a loop that will create arrays filled with zeros and I'd like it so that each array can be identified somehow (i.e. subscripts, unique name for each array).
In Maple, I would have done something like this… for k to 10 do A[k] := Array(1..10): end do:
which would create 10 arrays and each could be indivually accessed using A[k]. (i.e. A[1] is an array of size 10, A[2] is a different array of size 10 etc…)
Is there a way to do this in Matlab?

Best Answer

If all the arrays are vectors (=1D arrays in matlab) you can store them in a matrix (=2D array).
% Pre-allocate an matrix with four "vector arrays" of 6 elements each
A = zeros(4,6) ;
Here is an example to fill this array
for k=1:4
A(k,:) = (1:6) ./ k ; % just an example
end
To access a "array" (vector!) use indexing B = A(2,:) % get second row
If the vectors have different sizes, you need to look into cells and/or structs
A = {1:3 1:10 1:5} % a cell with 3 vectors
A{3} % third vector