4 个步骤:
%1.计算数据的中心
%2.从数据中减去中心
% 3. 将 2D 平面拟合到居中的数据
% 4. 把计算出来的中心加回去
首先,我要生成一组位于平面上的随机数据:
clear;close all;clc;
% Create a random set of data
mag = 20;
N = 1000;
A = 2 * mag * ( rand( N, 3 ) - 0.5 );
% We want to make sure that the data lies in a plane, so let's reduce it
% with the svd
[ U, S, V ] = svd(A,'econ');
S(3,3) = 0;
A = U * S * V.';
% Now we are going to offset the plane from the origin by a random point
p = 100 * rand(1,3);
A = A + repmat( p, N, 1 );
% Make sure we have the dataset we want by viewing from different angles
figure
subplot(1,3,1)
plot3(A(:,1),A(:,2),A(:,3),'.')
hold on
grid minor
view([1,1,1])
subplot(1,3,2)
plot3(A(:,1),A(:,2),A(:,3),'.')
hold on
grid minor
view([1,-1,1])
subplot(1,3,3)
plot3(A(:,1),A(:,2),A(:,3),'.')
hold on
grid minor
view([-1,1,1])
现在,此时,A 中的数据完全位于一个平面上。但是我们需要嘈杂的数据来确保算法正常工作。所以让我们重新添加噪音:
% Now let's add some noise to our data
B = A + randn(size(A));
现在我们开始为数据拟合平面......
% 1. Compute the center of the data
c = mean(B);
% 2. Subtract off the mean from the data
D = B - repmat( c, N, 1 );
% 3. Fit a 2D plane to the centered data
[ U, S, V ] = svd( D, 'econ' );
S(3,3) = 0;
D = U * S * V.';
% 4. Offset by the computed center
D = D + repmat( c, N, 1 );
% 5. Visualize by comparing with the noisy data
figure
subplot(1,3,1)
plot3(B(:,1),B(:,2),B(:,3),'.')
hold on
plot3(D(:,1),D(:,2),D(:,3),'.')
grid minor
view([1,1,1])
subplot(1,3,2)
plot3(B(:,1),B(:,2),B(:,3),'.')
hold on
plot3(D(:,1),D(:,2),D(:,3),'.')
grid minor
view([1,-1,1])
subplot(1,3,3)
plot3(B(:,1),B(:,2),B(:,3),'.')
hold on
plot3(D(:,1),D(:,2),D(:,3),'.')
grid minor
view([-1,1,1])