2014-02-10 51 views
2

我可以在Matlab中用一組x,y點繪製迴歸線。但是,如果我有一組點(如下圖),說我有四組點,我想爲它們繪製四條迴歸線......我該怎麼做?所有點都保存在x,y中。沒有辦法將它們分開,並將它們分成四組不同的變量。Matlab中的點集羣的迴歸行

看到下面的圖片。忽略傳說和標籤。任何想法如何在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'); 
+1

你可以發佈你的代碼,適用於一組o f分? – darthbith

+0

添加到帖子中。請檢查。 – ridctg

+0

你可以使用矩陣索引,比如'polyfit(x(1:10),y(1:10),1)'?爲什麼你不能將它們分解成單獨的變量? – darthbith

回答

3

你有你的價值觀分成集羣。這是一個非平凡的操作。這可以通過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

考慮到不同的順序(看着劇情的顏色) ,這些結果確實是你所期望的,因爲我戴上了很大程度的噪音:)

+0

嘿,感謝您提供的解決方案。它有一個問題。我有一些垂直傳播的點。所以,我希望迴歸線在特定情況下處於垂直方向。可能嗎?此外,我希望我的標記(點)透明....我沒有找到辦法做到這一點....讓我知道你是否可以幫助... – ridctg