tags:

views:

9956

answers:

5

Hello,

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.

+5  A: 

Use cell arrays:

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]);

If you absolutely can't know the number in advance, you could simply use MATLAB's dynamic indexing to make the array as large as you need, although it wouldn't have very good performance. For example:

myArray{1} = zeros(500, 800);
if twoRequired, myArray{2} = zeros(500, 800); end
Hosam Aly
A: 

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
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
I think you're correct.I had not come across cell before.
Rocco
A: 

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 = single(unknown, 500,800);

To append to that array try: myArrayOfMatrices(unknown+1,:,:) = zeros(500,800);

+6  A: 

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
A: 

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