|
5#
發(fā)表于 2023-4-22 07:33:18
|
只看該作者
function [eigvector, eigvalue, Y] = LDA(X,gnd)
old_X = X;
% ====== Initialization
[nSmp,nFea] = size(X); % 當(dāng)有兩個(gè)輸出參數(shù)時(shí),size函數(shù)將矩陣的行數(shù)返回到第一個(gè)輸出變量r,將矩陣的列數(shù)返回到第二個(gè)輸出變量c。 得到樣本的維數(shù)和個(gè)數(shù)
classLabel = unique(gnd); %取集合a的不重復(fù)元素構(gòu)成的向量
nClass = length(classLabel);%如果A為非空數(shù)組,返回行數(shù)和列數(shù)兩者之間數(shù)值較大的那一個(gè)值,即相當(dāng)于執(zhí)行了max(size(A))
bPCA = 0;
if nFea > (nSmp - nClass)
PCAoptions = [];
PCAoptions.ReducedDim = nSmp - nClass;
[eigvector_PCA, eigvalue_PCA, meanData, new_X] = PCA(X,PCAoptions);
X = new_X;
[nSmp,nFea] = size(X);
bPCA = 1;
end
sampleMean = mean(X);
MMM = zeros(nFea, nFea);
for i = 1:nClass,
index = find(gnd==classLabel(i));
classMean = mean(X(index, :));
MMM = MMM + length(index)*classMean'*classMean;
end
W = X'*X - MMM;
B = MMM - nSmp*sampleMean'*sampleMean;
W = (W + W')/2;
B = (B + B')/2;
option = struct('disp',0);
[eigvector, eigvalue] = eigs(B,W,nClass-1,'la',option);
eigvalue = diag(eigvalue);
for i = 1:size(eigvector,2)
eigvector(:,i) = eigvector(:,i)./norm(eigvector(:,i));
end
if bPCA
eigvector =eigvector_PCA*eigvector;
end
if nargout == 3
Y = old_X * eigvector;
end
LDA算法代碼看不懂 大神幫個(gè)忙 寫(xiě)點(diǎn)注釋啊 |
|