使用Matlab实现Parzen窗实验,其中:(1)N=10 h= 0.25 , 1, 4;(2)N=100 h= 0.25 , 1, 4;(3)N=1000 h= 0.25 , 1, 4
时间: 2024-05-21 22:10:10 浏览: 85
以下是一个简单的Matlab代码实现Parzen窗实验:
% 生成数据
data = [randn(1,10); randn(1,10)+3; randn(1,10)+6];
% 绘制原始数据散点图
figure
scatter(data(1,:),zeros(1,10),'r')
hold on
scatter(data(2,:),zeros(1,10),'g')
scatter(data(3,:),zeros(1,10),'b')
% 定义核函数
function y = kernel(x)
y = zeros(size(x));
y(abs(x)<=0.5) = 1;
end
% 定义Parzen窗函数
function y = parzen(x, data, h)
n = size(data,2);
y = zeros(size(x));
for i = 1:length(x)
for j = 1:n
y(i) = y(i) + kernel((x(i)-data(j))/h);
end
y(i) = y(i)/(n*h);
end
end
% 绘制Parzen窗函数图像
x = -5:0.1:11;
h = [0.25 1 4];
for i = 1:length(h)
y = parzen(x,data,h(i));
plot(x,y,'LineWidth',2)
end
% 添加图例和标签
legend('class 1','class 2','class 3','h=0.25','h=1','h=4','Location','northwest')
xlabel('x')
ylabel('p(x)')
阅读全文