You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用压缩感知处理二维矩阵时遇linsolve报错:矩阵需正定

Fixing "Matrix must be positive definite" Error in Compressive Sensing with l1-magic

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 phi is generated with randi(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 phi with the FFT matrix psi creates a theta matrix 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:42:55