如何解决警告:矩阵是单数,RCOND = NAN

时间:2015-03-02 18:03:51

标签: matlab

我在网上找到了一个代码,当我使用我的数据集进行测试时。它给了我如下警告:

  

在196的Linear_Discriminant_Classifier中   警告:矩阵是单数,接近单数或
  严重缩放。结果可能不准确。 RCOND = NaN。

     

在184的Linear_Discriminant_Classifier_6中   警告:矩阵是单数,接近单数或
  严重缩放。结果可能不准确。 RCOND = NaN。

此外,NaNacrerr个值。

我粘贴部分代码并指出警告线(第184行和第196行)。我不知道如何解决它。谁能给我一个帮助?

for cln=1:C
    nidx{cln} = find(data_tr(:,d+1)== cln);
    Xn = data_tr(nidx{cln},1:d);%data for class cln
    n{cln} = length(nidx{cln});
    %class mean
    m{cln}=sum(Xn)./(n{cln});
    Xnprod{cln} = Xn'*Xn;
end

%sample mean
Msum = 0;
for cln=1:C
    Msum = Msum + n{cln}*m{cln}; 
end
M = Msum/N;

%The common covariance matrix
Sw = 1/N*(Xnprod{1}+Xnprod{2}-n{1}*m{1}*m{1}' - n{2}*m{2}*m{2}');
Swinv = inv(Sw);                                 (line 184)

acc = 0;
c1dist = 0;
c2dist = 0;
for itr=1:size(data_te,1)
    x = data_te(itr,1:d);
    dect(itr) = (Swinv*(m{1}'-m{2}'))'*(x'-M');
    p1 = n{1}/N;
    p2 = n{2}/N;
    d2 = (m{1}'-m{2}')'*inv(Sw)*(m{1}'-m{2}');   (line 196)
    thr = ((p2-p1)/2)*((1+p1*p2*d2)/(p1*p2));
    if dect(itr) < thr
        cl_lab(itr)=2;
        dist = thr - dect(itr);%distance of sample form decision boundary
        %Determine how far a sample is from the decision boundary
        if cl_lab(itr) == data_te(itr,d+1)
            c1dist = c1dist + dist;%correct decision has positive dist
        else
            c1dist = c1dist - dist;
        end%if        
    else
        cl_lab(itr)=1;
        dist = dect(itr)-thr;
        %Determine how far a sample is from the decision boundary
        if cl_lab(itr) == data_te(itr,d+1)
            c2dist = c2dist + dist;%correct decision has positive dist
        else
            c2dist = c2dist - dist;
        end%if                
    end%if
    if cl_lab(itr) == data_te(itr,d+1)
        acc = acc+1;
    end%if

end%for   
%acc
accuracy(cvn) = acc/size(data_te,1)*100;    
Ncomb(cntr)=Ncomb(cntr)+size(data_te,1);%for weighing
totdist(cntr) = totdist(cntr)+c1dist+c2dist;
end%cvn

Ncomb(cntr)=Ncomb(cntr)/cvn;%average test set size
totdist(cntr)=totdist(cntr)/(cvn*Ncomb(cntr));%normalize dist with samples
acr(cntr) = sum(accuracy)/10;
err(cntr) = 1 - acr(cntr)/100;

cntr = cntr+1;

0 个答案:

没有答案