Principal component analysis


function [finalData]=pca2(t,n,target)%t is the original data set.and n is the number of features you need
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here
temp = t;
result = cov(temp);%calculate the covariance matrix of the temp' matrix
[vect value] = eig(result);% calculate the eignvector and the eignvalue
EigArray = eig(result);%calculate the eignvalue
eign=EigArray';
e_length = length(eign);%compute the length of the eignvalue vector

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%the following loops are to sort the eignvector and record the subscript
%corresponding to each eignvalue
for j=1:1:e_length
    val=-inf;
    for i=1:1:e_length
        if val<eign(i)
            val=eign(i);
            e_sub(j)=i;
        end  
    end
    eign(e_sub(j))=-inf;
end
featureVector=[vect(:,e_sub(1))];
% disp('feature is');
% featureVector
for m=2:1:n
    featureVector=[featureVector,vect(:,e_sub(m))];

end
    disp('feature is');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%calculate the meanVector of the temp'
meanVector = temp;
for i=1:1:100
    mean = 1/42*sum(meanVector(:,i));
    for j=1:1:42
        meanVector(j,i)=meanVector(j,i)-mean;
    end  
end
 
finalData=featureVector'*meanVector';%get the final data which is processed use PCA method

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值