使用压缩感知处理二维矩阵时遇linsolve报错:矩阵需正定
Hey there, let's break down why you're hitting that Error using linsolve: Matrix must be positive definite in your compressive sensing code, and walk through concrete fixes to resolve it.
Root Cause
The error comes from the l1eq_pd function (from the l1-magic toolbox) using linsolve under the hood, which requires its input matrix to be positive definite. Your current setup fails this requirement because:
- Your measurement matrix
phiis generated withrandi(M,N), which produces integer values and doesn't follow the core properties needed for compressive sensing (like the Restricted Isometry Property, RIP). - Multiplying this non-standard
phiwith the FFT matrixpsicreates athetamatrix whose associated normal matrix (theta'*theta) isn't positive definite.
Step-by-Step Solutions
1. Use a Valid Compressive Sensing Measurement Matrix
Replace the integer random matrix with a normalized Gaussian random matrix—this is the standard, reliable choice for compressive sensing, as it satisfies RIP and produces numerically stable matrices:
% Replace your original phi generation with this M = ceil(0.3*N); phi = randn(M, N) / sqrt(M); % Normalization ensures proper scaling for accurate measurements
2. Add Regularization to Enforce Positive Definiteness
Even with a Gaussian phi, numerical quirks might leave theta non-positive definite. Add a tiny identity matrix to theta to push all its eigenvalues above zero:
theta = phi * psi; theta = theta + 1e-6 * eye(size(theta)); % Small regularization term fixes definiteness without distorting results
Adjust the 1e-6 value if needed—keep it small enough to not alter your problem, but large enough to make the matrix positive definite.
3. Verify Matrix Properties (Optional)
Before running l1eq_pd, you can double-check if theta'*theta is positive definite by computing its eigenvalues:
eig_vals = eig(theta' * theta); min_eig = min(eig_vals); disp(['Minimum eigenvalue of theta''*theta: ', num2str(min_eig)]);
If min_eig is positive, your matrix is ready to use. If not, increase the regularization term slightly.
Corrected Full Code Snippet
Here's your code with all fixes applied:
Nf=800; N=401; E=E(Nf,N); % Your 2D real signal matrix (Nf x N) % Compressive sensing setup M=ceil(0.3*N); psi=fft(eye(N)); % Fix 1: Proper Gaussian measurement matrix phi = randn(M, N) / sqrt(M); EE = permute(E,[2 1]); theta=phi*psi; % Fix 2: Add regularization to ensure positive definiteness theta = theta + 1e-6 * eye(size(theta)); % Generate measurements for k=1:Nf y(:,k)=phi*EE(:,k); end x0 = theta.'*y; % Least squares initialization for l1eq_pd % Run L1 minimization for p=1:Nf X_hat(:,p) = l1eq_pd(x0(:,p), theta, [], y(:,p), 1e-5); end X1_hat=psi*X_hat; XX_hat=permute(X1_hat,[2 1]); % Complete permutation to match original E's shape
内容的提问来源于stack exchange,提问作者Neda

