Use cell arrays. This has an advantage over 3D arrays in that it does not require a contiguous memory space to store all the matrices. In fact, each matrix can be stored in a different space in memory, which will save you from Out-of-Memory errors if your free memory is fragmented. Here is a sample function to create your matrices in a cell array:

function result = createArrays(nArrays, arraySize)
    result = cell(1, nArrays);
    for i = 1 : nArrays
        result{i} = zeros(arraySize);
    end
end

To use it:

myArray = createArrays(requiredNumberOfArrays, [500 800]);

And to access your elements:

myArray{1}(2,3) = 10;

If you can't know the number of matrices in advance, you could simply use MATLAB's dynamic indexing to make the array as large as you need. The performance overhead will be proportional to the size of the cell array, and is not affected by the size of the matrices themselves. For example:

myArray{1} = zeros(500, 800);
if twoRequired, myArray{2} = zeros(500, 800); end
Answer from Hosam Aly on Stack Overflow
🌐
MathWorks
mathworks.com › matlab › get started with matlab
Matrices and Arrays - MATLAB & Simulink
All MATLAB variables are multidimensional arrays, no matter what type of data. A matrix is a two-dimensional array often used for linear algebra. To create an array with four elements in a single row, separate the elements with either a comma (,) or a space.
🌐
MathWorks
mathworks.com › matlab mobile › matlab mobile fundamentals
Creating Matrices and Arrays - MATLAB & Simulink
Create an array with four elements in a single column: >> a = [1; 2; 3; 4] a = 1 2 3 4 · To create a matrix that has multiple rows, separate the rows with semicolons.
🌐
MathWorks
mathworks.com › matlab › language fundamentals › matrices and arrays
Multidimensional Arrays - MATLAB & Simulink
A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns.
Top answer
1 of 6
72

Use cell arrays. This has an advantage over 3D arrays in that it does not require a contiguous memory space to store all the matrices. In fact, each matrix can be stored in a different space in memory, which will save you from Out-of-Memory errors if your free memory is fragmented. Here is a sample function to create your matrices in a cell array:

function result = createArrays(nArrays, arraySize)
    result = cell(1, nArrays);
    for i = 1 : nArrays
        result{i} = zeros(arraySize);
    end
end

To use it:

myArray = createArrays(requiredNumberOfArrays, [500 800]);

And to access your elements:

myArray{1}(2,3) = 10;

If you can't know the number of matrices in advance, you could simply use MATLAB's dynamic indexing to make the array as large as you need. The performance overhead will be proportional to the size of the cell array, and is not affected by the size of the matrices themselves. For example:

myArray{1} = zeros(500, 800);
if twoRequired, myArray{2} = zeros(500, 800); end
2 of 6
45

If all of the matrices are going to be the same size (i.e. 500x800), then you can just make a 3D array:

nUnknown;  % The number of unknown arrays
myArray = zeros(500,800,nUnknown);

To access one array, you would use the following syntax:

subMatrix = myArray(:,:,3);  % Gets the third matrix

You can add more matrices to myArray in a couple of ways:

myArray = cat(3,myArray,zeros(500,800));
% OR
myArray(:,:,nUnknown+1) = zeros(500,800);

If each matrix is not going to be the same size, you would need to use cell arrays like Hosam suggested.

EDIT: I missed the part about running out of memory. I'm guessing your nUnknown is fairly large. You may have to switch the data type of the matrices (single or even a uintXX type if you are using integers). You can do this in the call to zeros:

myArray = zeros(500,800,nUnknown,'single');
🌐
MathWorks
mathworks.com › matlab › language fundamentals › matrices and arrays
Creating, Concatenating, and Expanding Matrices - MATLAB & Simulink
The diag function places the input elements on the diagonal of a matrix. For example, create a row vector A containing four elements. Then, create a 4-by-4 matrix whose diagonal elements are the elements of A. ... The createArray function (since R2024a) can return arrays of almost any MATLAB ...
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1634550-how-to-create-an-array-of-matrices
How to create an array of matrices? - MATLAB Answers - MATLAB Central
January 23, 2022 - This only works when A, B and C have the same sidelenths. If not you need a cell array. Sign in to comment. Sign in to answer this question. Find more on Logical in Help Center and File Exchange ... Find the treasures in MATLAB Central and discover how the community can help you!
🌐
MathWorks
mathworks.com › matlabcentral › answers › 237795-create-a-matrix-with-existing-arrays
Create a matrix with existing arrays - MATLAB Answers - MATLAB Central
August 29, 2015 - https://www.mathworks.com/matlabcentral/answers/237795-create-a-matrix-with-existing-arrays#comment_307009 ... My pleasure. Using eval to put them into a matrix, as you intend, is the correct use of eval.
Find elsewhere
🌐
MathWorks
mathworks.com › matlab › mathematics › elementary math › arithmetic operations
Array vs. Matrix Operations - MATLAB & Simulink
The following table provides a summary of arithmetic array operators in MATLAB. For function-specific information, click the link to the function reference page in the last column. Matrix operations follow the rules of linear algebra and are not compatible with multidimensional arrays.
🌐
TutorialsPoint
tutorialspoint.com › matlab › matlab_arrays.htm
MATLAB - Arrays
In this section, we will discuss some functions that create some special arrays. For all these functions, a single argument creates a square array, double arguments create rectangular array. The zeros() function creates an array of all zeros − ... The eye() function creates an identity matrix.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 13018-array-of-matrices
Array of Matrices.? - MATLAB Answers - MATLAB Central
August 3, 2011 - How do I make the array. Also, can is there a way to multiply the A and B matrices without casting?? Sign in to comment. Sign in to answer this question. ... Regarding your first question: MATLAB supports N-d arrays, so you can store a variable as 576x576x100.
🌐
MathWorks
mathworks.com › videos › working-with-arrays-in-matlab-69022.html
Working with Arrays in MATLAB - MATLAB
You can delete one or more rows of an array such as rows 1 to 2, all the columns, by assigning them to the empty matrix denoted by square brackets. A is now two rows shorter. Sometimes it is convenient to treat two dimensional arrays such as these as a one dimensional array as though all the columns were stacked together into a single column and specify single index. This is called linear indexing. For example, the element at row 1 column 2 can be accessed through one linear index, 5. This is possible because MATLAB arrays are stored column wise in memory.
Published   June 29, 2020
🌐
MathWorks
mathworks.com › matlab › language fundamentals › matrices and arrays
ones - Create array of all ones - MATLAB
X = ones(n) returns an n-by-n matrix of ones. ... X = ones(sz1,...,szN) returns an sz1-by-...-by-szN array of ones where sz1,...,szN indicates the size of each dimension.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 359232-putting-a-matrix-into-an-array
Putting a matrix into an array - MATLAB Answers - MATLAB Central
October 1, 2017 - I have a series of objects that ... into an array to keep them together. However, upon initializing such an object with a function, it returns a 2-D matrix, and the program gives me an error: "Subscripted assignment dimension mismatch.". This is the code: ... I hope you understand what I'm trying to do, and what the problem is. ... Sign in to comment. Sign in to answer this question. ... Where N is the number of matrices you will have. ... https://www.mathworks.com/matlabcentral/answers/3...