clc;
clearall;
closeall;
%%----BuildatrainingsetofasimilarversionofXOR
c_1=[00];
c_2=[11];
c_3=[01];
c_4=[10];
n_L1=20;%numberoflabel1
n_L2=20;%numberoflabel2
A=zeros(n_L1*2,3);
A(:,3)=1;
B=zeros(n_L2*2,3);
B(:,3)=0;
%createrandompoints
fori=1:n_L1
A(i,1:2)=c_1+rand(1,2)/2;
A(i+n_L1,1:2)=c_2+rand(1,2)/2;
end
fori=1:n_L2
B(i,1:2)=c_3+rand(1,2)/2;
B(i+n_L2,1:2)=c_4+rand(1,2)/2;
end
%showpoints
scatter(A(:,1),A(:,2),[],'r');
holdon
scatter(B(:,1),B(:,2),[],'g');
X=[A;B];
data=X(:,1:2);
label=X(:,3);
%%Usingkmeanstofindcintervector
n_center_vec=10;
rng(1);
[idx,C]=kmeans(data,n_center_vec);
holdon
scatter(C(:,1),C(:,2),'b','LineWidth',2);
%%Calulatesigma
n_data=size(X,1);
%calculateK
K=zeros(n_center_vec,1);
fori=1:n_center_vec
K(i)=numel(find(idx==i));
end
%UsingknnsearchtofindKnearestneighborpointsforeachcentervector
%thencalucatesigma
sigma=zeros(n_center_vec,1);
fori=1:n_center_vec
[n,d]=knnsearch(data,C(i,:),'k',K(i));
L2=(bsxfun(@minus,data(n,:),C(i,:)).^2);
L2=sum(L2(:));
sigma(i)=sqrt(1/K(i)*L2);
end
%%Calutateweights
%kernelmatrix
k_mat=zeros(n_data,n_center_vec);
fori=1:n_center_vec
r=bsxfun(@minus,data,C(i,:)).^2;
r=sum(r,2);
k_mat(:,i)=exp((-r.^2)/(2*sigma(i)^2));
end
W=pinv(k_mat'*k_mat)*k_mat'*label;
y=k_mat*W;
%y(y>=)=1;
%y(y<)=0;
%%trainingfunctionandpredictfunction
[W1,sigma1,C1]=RBF_training(data,label,10);
y1=RBF_predict(data,W,sigma,C1);
[W2,sigma2,C2]=lazyRBF_training(data,label,2);
y2=RBF_predict(data,W2,sigma2,C2);
扩展资料
matlab的特点
1、具有完备的图形处理功能,实现计算结果和编程的可视化;
2、友好的用户界面及接近数学表达式的自然化语言,使学者易于学习和掌握;
3、功能丰富的应用工具箱(如信号处理工具箱、通信工具箱等) ,为用户提供了大量方便实用的处理工具。
参考资料来源:百度百科—MATLAB