out of memory

3 views (last 30 days)
Muhammad
Muhammad on 17 Jan 2012
I am trying to find covariance matrix of a matrix of size 3 by 65536, and getting "out of memory" error. I want to avoid this error please suggest some way to get rid of this error.
Thanks

Accepted Answer

Anton Semechko
Anton Semechko on 18 Jan 2012
Here is an example:
N=6E4; % number of variables
M=10; % number of samples
% Simulate M samples and store them into one matrix
X=zeros(N,M);
for i=1:M
X(:,i)=[4*randn(3E4,1);randn(3E4,1)];
end
% Compute the mean and center
Xave=mean(X,2);
X=bsxfun(@minus,X,Xave);
Now your covariance matrix would be defined as C=(X*X')/(M-1). Because N>>>M, this covariance matrix will only have (M-1) non-zero eigenvalues. Since we want to avoid computing C explicitly, we can get the required eigen-decomposition with the use of SVD:
[P,D,~] = svd(X/sqrt(M-1),0);
P(:,M)=[];
D=diag(D);
D(M)=[];
where P is N-by-(M-1) matrix containing the eigenvectors of C and D is the list of corresponding eigenvalues.
  6 Comments
Muhammad
Muhammad on 18 Jan 2012
Thanks alot Anton Semechko
Xintao
Xintao on 27 Aug 2013
If I do it in the following way, then the results should be the same? That is: P=P1 and D=D1?
C=(X*X')/(M-1);
[P1,D1,~] = svd(C);

Sign in to comment.

More Answers (1)

Anton Semechko
Anton Semechko on 17 Jan 2012
I am assuming what you really meant to say is that you have 65536 data points in R3 and you are trying to compute the covariance matrix of size 65536. Unless your machine has at the minimum (65536^2)*8*3/2^20 =98304 GB of memory you will not be able to do that.
There are ways around it, however. It all depends on why you need the covariance matrix to begin with. If for instance you are going to perform orthogonal decomposition (i.e. find the eigenvalues and eigenvectors), then this operation can be done with the help of SVD which does not require explicit calculation of the covariance matrix.
  3 Comments
Muhammad
Muhammad on 17 Jan 2012
Thanks Anton Semechko for answering
Basically I am trying to find PCA, for that I first have to find covariance matrix. I want to know what should I do to avoid "out of memory" error, by increasing the size of virtual memory or RAM or something else, please guide me in this regard. Thanks
Walter Roberson
Walter Roberson on 17 Jan 2012
There are routines in the stats toolbox, but I do not know if they have ways of reducing memory; see http://www.mathworks.com/help/toolbox/stats/brkgqnt.html#f75476
But yes, make sure you use a 64 bit version of MATLAB, push up your virtual memory to at least 100 gigabyte (256 Gb would be safer), and let it go. It probably won't take more than 4 years.

Sign in to comment.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!