例如,我有两组数据,它们都是二维的。现在的问题是如何将这两组数据投影到一条线性方程上。
%%% Here is the data %%%%%% The data are Gaussian distributed with means m1 and m2, %%%%%% and have a common covariance matrix C %%%m1 = [0 3]'; %%% mean of data set 1m2 = [3 2.5]'; %%% mean of data set 2C1 = [2 1;1 2]; %%% covariance matrix %%%C2 = [2 1;1 2]; %%% covariance matrix %%%X1 = mvnrnd(m1,C1,N);X2 = mvnrnd(m2,C2,N);plot(X1(:,1),X1(:,2),'bx',X2(:,1),X2(:,2),'ro');grid on
回答:
这里提供了一个解决方案,假设你想要将数据正交投影到一条线上。这可能不是最有效的实现方式,但至少步骤易于理解。
这条线由参数 a
和 b
定义为:y = a*x + b;
N = 3;%%% Here is the data %%%%%% The data are Gaussian distributed with means m1 and m2, %%%%%% and have a common covariance matrix C %%%m1 = [0 3]'; %%% mean of data set 1m2 = [3 2.5]'; %%% mean of data set 2C1 = [2 1;1 2]; %%% covariance matrix %%%C2 = [2 1;1 2]; %%% covariance matrix %%%X1 = mvnrnd(m1,C1,N);X2 = mvnrnd(m2,C2,N);figureplot(X1(:,1),X1(:,2),'bx',X2(:,1),X2(:,2),'rx');grid onaxis equal% equation of the line% y = a*x + b a = 1;b = 1;% normalized directional vector of the lines = [a, a^2];s = s./norm(s);% translate points for easier workX1(:,2) = X1(:,2)-b;X2(:,2) = X2(:,2)-b;% projectionX1p = (X1*s')*s;X2p = (X2*s')*s;% tranlate the points backX1p(:,2) = X1p(:,2)+b;X2p(:,2) = X2p(:,2)+b;xx = -4:8;hold onplot(xx, a*xx+b, 'green')plot(X1p(:,1),X1p(:,2),'bo',X2p(:,1),X2p(:,2),'ro')xlabel('x')ylabel('y')title('Orthogonal projection')
x
是原始点,而 o
是它的投影。