MATLAB Answers

Out of Memory error when removing columns of a big matrix .

15 views (last 30 days)
Ozge Ozcakir
Ozge Ozcakir on 24 Jan 2020 at 11:20
Edited: James Tursa on 24 Jan 2020 at 17:20
I have constructed a big matrix A in MATLAB ( 120000 x 120000) without any issue. However, I am getting out-of-memory error when i try to remove few columns of this matrix using A(:,1:10)=[]. I am guessing, MATLAB is creating a temporary matrix while doing this operation and thats why i am getting an error.
Is there a way to overcome this issue?

  1 Comment

Walter Roberson
Walter Roberson on 24 Jan 2020 at 11:27
Yes, MATLAB does create a temporary matrix for this purpose.
I do not know if there is any alternative, even using mex; I do not know if there is a realloc() equivalent that can give back trailing memory.
It would possibly be easier if you were to fill in the shortened matrix with 0 or NaN so that the columns were compacted but the array stayed the same size.

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 24 Jan 2020 at 17:20
Edited: James Tursa on 24 Jan 2020 at 17:20
What are you doing with the matrix A downstream in your code? Even if the column removal worked, seems like you still might have problems downstream when using A in a calculation which would create another large variable.
There is no easy mex solution for this either, since you can't set the data pointer of a variable to point to the interior of an allocated memory block legally. The only workaround I can think of is this FEX mex submission which uses unofficial hacks to point at the interior of a variable without a data copy:


Sign in to comment.

Sign in to answer this question.