Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of Matrices in MATLAB

I am looking for a way to store a large variable number of matrixes in an array in MATLAB.

Are there any ways to achieve this?

Example:

for i: 1:unknown   myArray(i) = zeros(500,800); end 

Where unknown is the varied length of the array, I can revise with additional info if needed.

Update: Performance is the main reason I am trying to accomplish this. I had it before where it would grab the data as a single matrix, show it in real time and then proceed to process the next set of data.

I attempted it using multidimensional arrays as suggested below by Rocco, however my data is so large that I ran out of Memory, I might have to look into another alternative for my case. Will update as I attempt other suggestions.

Update 2: Thank you all for suggestions, however I should have specified beforehand, precision AND speed are both an integral factor here, I may have to look into going back to my original method before trying 3-d arrays and re-evaluate the method for importing the data.

like image 648
user57685 Avatar asked Jan 21 '09 20:01

user57685


People also ask

Can you have an array of matrices in MATLAB?

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. Each element is defined by two subscripts, the row index and the column index.

How do you create an array of matrices?

To create an array with four elements in a single row, separate the elements with either a comma ( , ) or a space. This type of array is a row vector. To create a matrix that has multiple rows, separate the rows with semicolons. Another way to create a matrix is to use a function, such as ones , zeros , or rand .

What is an array in matrix?

An array is a vector with one or more dimensions. A one-dimensional array can be considered a vector, and an array with two dimensions can be considered a matrix. Behind the scenes, data is stored in a form of an n-dimensional matrix.

How do I create an array of ones in MATLAB?

X = ones( sz ) returns an array of ones where the size vector, sz , defines size(X) . For example, ones([2,3]) returns a 2-by-3 array of ones. X = ones(___, typename ) also specifies the data type (class) of X for any of the previous syntaxes. For example, ones(5,'int8') returns a 5-by-5 matrix of 8-bit integers.


1 Answers

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 
like image 107
Hosam Aly Avatar answered Sep 20 '22 18:09

Hosam Aly