English 中文(简体)
是否可能只节省一半的计量矩阵,以拯救记忆?
原标题:Is it possible to save only half of a symmetric matrix to save the memory?

在<代码>Ax=b类型问题上使用了大量矩阵。 <>A>/strong>是不对称的。 是否有任何算法让我们只节省一半的基数,并像<条码>x=A那样运作?

最佳回答

You ll only save half the memory, but you can do this by creating a flat version of the matrix, saving that, then unflattening it. The extra time required probably doesn t make the saving worthwhile, mind:

% pretend this is symettric...
A = rand(10, 10);

% store it as a flat list
flatA = [];
for k = 1:size(A,1)
    flatA = [flatA; A([1:k],k)];
end

save( flatA.mat ,  flatA );

% undo
N = (-1 + (1+4*2*length(flatA))^0.5)/2;
newA = NaN(N,N); 
cur = 1;
for k = 1:N
    len = k;
    newA(1:len, k) = flatA(cur:cur+len-1);
    cur = cur + len;
end
% real A cannot have NaNs or this trick fails
newA(isnan(newA)) = newA(isnan(newA )) ;
问题回答

Here s an idea, but I haven t tested it out. If your symmetric matrix is positive definite, do a Cholesky decomposition of the symmetric matrix, A, giving you A = U*U . If you store U as a sparse matrix using MATLAB s builtin sparse matrix, you have everything you need, and you ve used roughly half the memory. Since its using MATLAB s sparse matrix type, you have operate on it using standard MATLAB functions, as long as you remember that A = U*U

For example, to compute A*x = b, use x = U U. Unlike the other proposed solutions, MATLAB will never be actually using a full matrix internally, and will even use accelerated solvers that will take advantage of the fact that you re only solving with triangular. The cost is that to solve a single system, you ve actually running the backslash operator twice (see above). However, that s the price you pay for never instantiating the full matrix.

If you extract the upper triangular part and convert to a sparse matrix, it should save memory. This technique is reasonably quick.

% Create a symmetric matrix
A = rand(1000);
A = A + A ;

% Extract the upper triangular part
B = sparse(triu(A))              % This is the important line, the rest is just checking.

% Undo
BB = full(B);
C = BB + BB  - diag(diag(BB));

% Check correct
assert(all(A(:) == C(:)));

% Save matrices
save( A.mat ,  A );
save( B.mat ,  B );

% Get file sizes
infoA = dir( A.mat ); infoA.bytes
infoB = dir( B.mat ); infoB.bytes

EDIT to clarify things for woodchips

The above solution demonstrates a way of saving matrices with less file space. The matrix B also takes up less memory than the original matrix A. If you want to do linear algebraic operations on B, that works perfectly well. Compare

b = rand(1000);
fullTriUA = triu(A);
sparseTriUA = sparse(fullTriUA);  %same as B above
fullResult = fullTriUA;
sparseResult = sparseTriUA;
assert(all(fullResult(:) == sparseResult(:)))




相关问题
Matrix to Represent a Triangle in Screen Space

So i have a set of four points in 3D Space. P1 [0, 0, 0] P2 [128, 0, 0] P3 [0, 128, 0] P4 [128, 128, 0] Which I m then projecting orthographically to the screen effectively giving me two ...

Multiply a 3D matrix with a 2D matrix

Suppose I have an AxBxC matrix X and a BxD matrix Y. Is there a non-loop method by which I can multiply each of the C AxB matrices with Y?

matrix and vector template classes in c++

#include <array> template <typename T> class Vector4<T> { std::array<T, 4> _a; // or T _a[4]; ? }; template <typename T> class Matrix4<T> { std::array<...

Linear Independence Matrix

Suppose we have a m by n matrix A with rank m and a set K⊆{1..n} such that the columns of A indexed by K are linearly independent. Now we want to extend K and find a set L so that k⊆L and columns ...

Difference between MATLAB s matrix notations

How do you read the following MATLAB codes? #1 K>> [p,d]=eig(A) // Not sure about the syntax. p = 0.5257 -0.8507 -0.8507 -0.5257 d = ...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Java large datastructure for storing a matrix

I need to store a 2d matrix containing zip codes and the distance in km between each one of them. My client has an application that calculates the distances which are then stored in an Excel file. ...

Checking row and column for a word in python

I am trying to create a checking program to see if the word is in a matrix horizontally or vertically. I have the code for checking the row, but would checking the column be similar to the row code? ...

热门标签