user2287094
user2287094

Reputation: 277

Concatenate more than two matrices - get out of memory error

I would like to concatenate some matrices (20-30). I wrote a simple function:

function [ appendedResults ] = appendResults( startNumber, endNumber  )

str2 = '_simulation.xlsx';
str1 = num2str(startNumber);
InputFilename = strcat(str1, str2);     

appendedResults =  uint16( xlsread(InputFilename ) );

for i= startNumber+1  :  endNumber
    temp_simulationResults = uint16( xlsread(InputFilename ) );
    appendedResults = vertcat( appendedResults, temp_simulationResults );
end

filename1 = strcat('appended',  str2);  
xlswrite(filename1, appendedResults);

end

It's works fine if I use 3-4 matrices. But when I'm using more, I get an error:

Error using xlswrite (line 219)
Error: Not enough memory

Then the matlab use 6 GigaByte (!!) memory. The matrices are only 7 MegaByte!

I wrote another function with for loops( without Matlab Vertcat function). I get the same error!

function [ appendedResults ] = appendResultsInOneMatrix( startNumber, endNumber )

str2 = '__simulation.xlsx';
str1 = num2str(startNumber);
InputFilename = strcat(str1, str2);     

numberOfItems = (endNumber - startNumber) + 1 ;

firstResults =   uint16(  xlsread(InputFilename ) );
[row , col] = size(firstResults);

appendedResults =   zeros ( row * numberOfItems , col);

for i=1: row
    for j=1:col
        appendedResults( i , j ) = firstResults( i , j);
    end
end

for i= startNumber+1 : endNumber

    str1 = num2str(i);
    InputFilename = strcat(str1, str2);

    temp_simulationResults = uint16( xlsread(InputFilename ) );
    [temp_row , temp_col] = size(temp_simulationResults);

    for m = 1 : temp_row
           for n = 1 :  temp_col

               appendedResults( m+row , n ) = temp_simulationResults( m , n);
           end

    end

end


    filename1 = strcat('appended',  str2); 
    xlswrite(filename1, appendedResults);

end

What's wrong?

Upvotes: 1

Views: 224

Answers (1)

Marcus Müller
Marcus Müller

Reputation: 36337

If you concatenate matrices, the resulting dimensions will be the maxima of the individual dimensions of the input matrices times the number of matrices; for example, if matrix A is 1x1,000,000 and matrix B is 1,000,000x1, the individual storage of these matrices would only be 1 million elements, but the resulting matrix would need to store 2* 1012 elements!

EDIT: also, Excel files definitely are not the storage you should be using for tables with more than a million entries!

EDIT2: Take a piece of paper. Draw a 1cm x 10cm (width x height) rectangle on it, write "matrix A" on it so you know how the rectangle is oriented and don't accidentially rotate it. Cut it out. Draw a 10cm x 1cm rectange, write "matrix B" on it, for the same reason.

Concatenate both rectangles. What is the area of the rectangle you need to enclose this shape? What's the sum of areas of the "matrix A" and "matrix B" rectangles?

Matrices are rectangular, and by default you will have to have enough space to store the minimum rectangle that contains the concatenation of your matrices. You can change that by using sparse matrices in Matlab (that's a long topic, and can't be explained in a single StackOverflow answer), but that won't change the fact that what you're doing probably doesn't make the least sense.

Upvotes: 1

Related Questions