Gze
Gze

Reputation: 408

How can I create big matrix in Matlab

I need to create a matrix of Matlab, it's big matrix as below:

X = zeros(128,2e7); 

when I run the command, it gives me an error of

Out of memory. Type HELP MEMORY for your options.

Is there a way to avoid that error?

Thank you

Upvotes: 0

Views: 90

Answers (3)

Daniel
Daniel

Reputation: 36710

You are running the code

X = zeros(128,2e7); 

An array of this size requires 128*2e7*8 Byte of storage. This is about 20GB. Considering an average PC, you probably don't have 20GB of ram available for MATLAB. The direct answer to your question is, NO you can not use more RAM than your PC has available.

Possible strategies are:

  • Do you really need all the elements in RAM at the same time? Maybe you can keep the Matrix on disk and process it piece by piece?
  • Do you need double precision? This is the default data type in Matlab. Maybe single or an integer data type would be sufficient. What do you want to store in each element of the matrix?

Upvotes: 1

Zeyad_Zeyad
Zeyad_Zeyad

Reputation: 250

Try this

zeros(128,2e7,'single');  

It can work with you

Upvotes: 1

Emrah Diril
Emrah Diril

Reputation: 1755

If it fits your use case, you could use a sparse matrix

There is also tall arrays

Upvotes: 3

Related Questions