gpt4 book ai didi

matlab - Matlab 中点簇的回归线

转载 作者:行者123 更新时间:2023-12-05 03:14:59 28 4
gpt4 key购买 nike

我可以在 Matlab 中用一组 x,y 点绘制回归线。但是,如果我有一组点(如下图),假设我有四组点,我想为它们绘制四条回归线……我该怎么做?所有的点都保存在 x,y 中。没有办法将它们分开并将它们放入四个不同的变量集。

见下图。忽略图例和标签。知道如何在 Matlab 中执行此操作吗?如果只有一个集群,我可以做到。但我想一次为所有四个集群做。 enter image description here

我现在为一个集群使用的代码:

 %----------- 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');

最佳答案

您必须将您的值分成几类。这是一个非常重要的操作。这可以通过统计工具箱中的 kmeans 来完成,例如:

%// First, I generate some example data in 4 clusters. 

%// intercepts
a = [4 7 0 -5];

%// slopes
b = [0.7 1.0 1.0 0.8];

%// ranges
xmin = [+1 -6 -6 +1];
xmax = [+6 -1 -1 +6];

%// generate clusters
N = [30 40 25 33];
X = arrayfun(@(ii) (xmax(ii)-xmin(ii))*rand(N(ii),1) + xmin(ii), 1:4, 'UniformOutput', false);
Y = arrayfun(@(ii) a(ii) + b(ii)*X{ii} + randn(size(X{ii})), 1:4, 'UniformOutput', false);


%// Unfortunately, your points not are given in 4 separate clusters, but
%// in a single array:
X = cat(1,X{:});
Y = cat(1,Y{:});

%// Therefore, you'll have to separate the data again into clusters:
idx = kmeans([X,Y], 4, 'Replicates', 2);

X = {
X(idx==1)
X(idx==2)
X(idx==3)
X(idx==4)
};

Y = {
Y(idx==1)
Y(idx==2)
Y(idx==3)
Y(idx==4)
};


%// Now perform regression on each cluster
ab = arrayfun(@(ii) [ones(size(X{ii})) X{ii}]\Y{ii}, 1:4, 'UniformOutput', false);

%// the original values, and the computed ones
%// note that the order is not the same!
[a; b]
[ab{:}]

%// Plot everything for good measure
figure(1), clf, hold on

plot(...
X{1}, Y{1}, 'g.',...
X{2}, Y{2}, 'b.',...
X{3}, Y{3}, 'r.',...
X{4}, Y{4}, 'c.')

line([min(X{1}); max(X{1})], ab{1}(1) + ab{1}(2)*[min(X{1}); max(X{1})], 'color', 'k')
line([min(X{2}); max(X{2})], ab{2}(1) + ab{2}(2)*[min(X{2}); max(X{2})], 'color', 'k')
line([min(X{3}); max(X{3})], ab{3}(1) + ab{3}(2)*[min(X{3}); max(X{3})], 'color', 'k')
line([min(X{4}); max(X{4})], ab{4}(1) + ab{4}(2)*[min(X{4}); max(X{4})], 'color', 'k')

结果:

ans =
4.0000 7.0000 0 -5.0000
0.7000 1.0000 1.0000 0.8000
ans =
-4.6503 6.4531 4.5433 -0.6326
0.7561 0.8916 0.5914 0.7712

enter image description here

考虑到不同的顺序(查看图中的颜色),这些结果确实是你所期望的,因为我放了很大程度的噪音 :)

关于matlab - Matlab 中点簇的回归线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21667531/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com