我可以在Matlab中用一组x,y点绘制回归线。但是,如果我有一组点(如下图),说我有四组点,我想为它们绘制四条回归线......我该怎么做?所有点都保存在x,y中。没有办法将它们分开,并将它们分成四组不同的变量。Matlab中的点集群的回归行
看到下面的图片。忽略传说和标签。任何想法如何在Matlab中做到这一点?如果只有一个群集,我可以做到。但我想一次为所有四个集群做。我现在使用的一个集群
代码:
%----------- Linear regression -----------------
p= polyfit(x,y,1);
f= polyval(p,x);
%----------- Call R-square function ------------
r2=Rsquare(x,y,p);
%------------- Plot data -----------------------
figure()
plot(x,y,'*k');hold on
plot(x,f,'-r'); % show linear fit
xlabel('index');
ylabel('Intensity a.u.');
title('Test: Linear regreesion && R-square');
%------- Show y-data on current figure ---------
[row col]=size(y);
for i=1:col
str=num2str(y(i));
text(x(i),y(i),str,'Color',[0 0 1]);
end
%--Show linear equation on current figure -------
m1=num2str(p(1));c1=num2str(p(2));Rsquare1=num2str(r2(1));
text(1.05,80,['y= ',m1,'x+',c1,' , R^2= ',Rsquare1,'.'],'FontSize',10,'FontName','Times New Roman');
你可以发布你的代码,适用于一组o f分? – darthbith
添加到帖子中。请检查。 – ridctg
你可以使用矩阵索引,比如'polyfit(x(1:10),y(1:10),1)'?为什么你不能将它们分解成单独的变量? – darthbith