out of memory
조회 수: 5 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Anton Semechko
2012년 1월 18일
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
Xintao
2013년 8월 27일
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);
추가 답변 (1개)
Anton Semechko
2012년 1월 17일
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
Walter Roberson
2012년 1월 17일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!