MATLAB: How to specify a symmetric matrix so that MATLAB does not need to create and store the redundant symmetric part

efficientefficientlymatrixmemorypartredundantsignificantsparsestoragestoresymmetric

I have an N-by-N symmetric matrix that is very large and requires a large amount of memory to store. However, of the "N^2" elements in the matrix, "N*(N – 1)/2" are redundant. I would like to be able to create and operate on this symmetric matrix without ever having to create and store the entire N-by-N matrix.
LAPACK supports this type of matrix storage, and is referred to as a "packed" matrix.
I also have another sparse symmetric matrix and would like to store only half of it as opposed to the entire matrix.

Best Answer

The ability to store and operate on only the significant part of a symmetric or a sparse symmetric matrix is not available in MATLAB.
The following symmat2vec.m function extracts and stores the significant part of an N-by-N symmetric matrix within a "N*(N + 1)/2"-by-1 vector. However, you will not be able to operate on this vector as a symmetric matrix without recreating the entire N-by-N symmetric matrix using the following vec2symmat.m function.
function V = symmat2vec(A)
% Converts a symmetric matrix "A" to a vector "V" of its significant part
N = size(A, 1);
V = zeros(N/2*(N + 1), 1);
I = [0 cumsum(1:N)];
for m = 1:N
V(I(m)+1:I(m + 1)) = A(1:m, m);
end
function A = vec2symmat(V)
% Converts vector "V" to an N-by-N matrix "A"
n = length(V);
N = (-1 + sqrt(1 + 8*n))/2;
A = zeros(N);
I = [0 cumsum(1:N)];
for m = 1:N
A(1:m, m) = V(I(m)+1:I(m + 1));
end
% Insert symmetric part
for m = 1:N-1
A(m+1:N, m) = A(m, m+1:N).';
end