40

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.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
user57685
  • 411
  • 1
  • 4
  • 6
  • I guess using cell matrices will not affect performance at all. Since they essentially contain pointers to the actual matrices, expanding them (even with dynamic indexing) should not incur performance overheads (except for minimally reallocating an array of pointers, which is negligible). – Hosam Aly Jan 21 '09 at 22:01

6 Answers6

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
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
  • @HosamAly I was creating 2D array of matrices based on your proposal. Then, I was proposed to create a single 4D array which outperforms the former by 9x, but I get a memory limit problem with big matrix sizes. Do you have any proposal why so? Thread here http://stackoverflow.com/q/40052664/54964 – Léo Léopold Hertz 준영 Oct 15 '16 at 07:44
  • 1
    @Masi Sorry, I don't know. – Hosam Aly Oct 22 '16 at 09:11
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');
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • 3
    The problem with using a 3D array is that it requires a contiguous space in memory. Although it can have better performance than cell arrays, the latter allows the allocation of the 2D matrices in different memory spaces, thus making it more suitable if used with a fragmented memory. – Hosam Aly Nov 02 '11 at 15:41
1
myArrayOfMatrices = zeros(unknown,500,800);

If you're running out of memory throw more RAM in your system, and make sure you're running a 64 bit OS. Also try reducing your precision (do you really need doubles or can you get by with singles?):

myArrayOfMatrices = zeros(unknown,500,800,'single');

To append to that array try:

myArrayOfMatrices(unknown+1,:,:) = zeros(500,800);
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
0

I was doing some volume rendering in octave (matlab clone) and building my 3D arrays (ie an array of 2d slices) using

buffer=zeros(1,512*512*512,"uint16");
vol=reshape(buffer,512,512,512);

Memory consumption seemed to be efficient. (can't say the same for the subsequent speed of computations :^)

timday
  • 24,582
  • 12
  • 83
  • 135
-2

just do it like this

x=zeros(100,200);
for i=1:100
  for j=1:200
    x(i,j)=input('enter the number');
  end
end
AlBlue
  • 23,254
  • 14
  • 71
  • 91
dimuthu93
  • 3
  • 1
-2

if you know what unknown is,

you can do something like

myArray = zeros(2,2);
for i: 1:unknown
  myArray(:,i) = zeros(x,y);
end

However it has been a while since I last used matlab. so this page might shed some light on the matter :

http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_prog/f1-86528.html

Rocco
  • 1,395
  • 3
  • 11
  • 19
  • 1
    You need a 3-dimensional array to do this. Your example would cause an error for mismatching array dimensions. You probably mean `myArray(:,:,i)`, although I'm not sure how it would behave (as I don't have MATLAB now). – Hosam Aly Jan 21 '09 at 21:24