MATLAB: How can prelocate arrays

prelocating arrays

I have an array like A which I want to have all of its elements zero by using Matlab features like zeros(x) function.
for example:
A=[1*3][1*3][1*3][1*3]
Each 1*3 matrix in the array A should have 3 zero.
I know how to make matrices with zero elements e.g zeros(2,3) but I don't know how to use it for array shown above with Matlab features without using simple loops.

Best Answer

This method uses a shared data copy of [0 0 0] to populate the individual cell elements, so it is memory efficient:
A = cell(1, n); % Or whatever size you need
A(:) = {[0 0 0]};
However, pre-allocating cell elements only makes sense in certain cases. E.g., it does not make sense if you are simply going to overwrite the elements with something else downstream in your code. How is the array A going to be used downstream in your code?