www.pudn.com > RobustSF.zip > TrainDictionary.m, change:2014-09-01,size:1463b


function [ DD ] = TrainDictionary(Xt,tr_lable,nclass,singleclass) 
addpath('KSVD_Matlab_ToolBox');%KSVD训练字典的工具包 
%Step4.KSVD训练字典,是一类一类的训练,然后把每一类的字典拼排在一起 
%设置字典参数 
param.L = 3; % number of elements in each linear combination. 
param.K = singleclass; % number of dictionary elements 
param.numIteration = 20; % number of iteration to execute the K-SVD algorithm. 
param.errorFlag = 0; % decompose signals until a fix number of coefficients reached. 
param.errorGoal = 1e-7;%spicilizede the must reached error. 
param.preserveDCAtom = 0;%if =1 then the first atom in the dictionary  is set to be constant, and does not 
                         %ever change. This  might be useful for working with natural images (in this case, only param.K-1 atoms are trained). 
param.InitializationMethod =  'DataElements';% spilized the initial dictionary with the signals themselves 
param.displayProgress =0;%show message for process 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
base=1; 
DD=zeros(size(Xt,1),param.K*nclass);%保存训练得到的字典 
disp('Starting to  train the dictionary'); 
tic; 
for i=1:nclass 
    len=length(find(tr_lable==i)); 
    base2=base+len-1; 
    X=Xt(:,base:base2); 
    base=base2+1; 
    [Dictionary,~]  = KSVD(X,param);%训练第i类的字典 
    DD(:,(i-1)*param.K+1:i*param.K)=Dictionary(:,:); 
end 
tim=toc; 
fprintf('Dictionary use time %15.5f.\n',tim); 
disp('The KSVD algorithm retrived '); 
end