CoderInNetwork
CoderInNetwork

Reputation: 3093

How to create a very large sparse identity matrix in Matlab?

I am trying to create a 75000*75000 identity matrix in Matlab with the code:

sparse(eye(75000))

and I get the following error:

Requested 75000x75000 (41.9GB) array exceeds maximum array size preference. Creation of arrays
greater than this limit may take a long time and cause MATLAB to become unresponsive. See array
size limit or preference panel for more information.

I know the reason for the error, but but how can I create such a sparse matrix in Matlab?

Upvotes: 0

Views: 247

Answers (1)

sco1
sco1

Reputation: 12214

sparse(eye(75000));

Requires eye(75000) be stored in memory. You want to use speye to avoid the intermediate step:

speye(75000);

I'd also recommend reading the documentation for Sparse Matrix Creation.

Upvotes: 2

Related Questions