www.mediafire.c...
www.mathworks....
--------------------------------------------------------------------------------------------
clear;clc;
load('data.txt');figure(1);scatter3(data(:,1),data(:,2),data(:,3),'.')
%%
hold on;scatter3(42.706,88.286,70.656,'diamond','r')
PCA=pca(data);%use Principal component analysis method
e1=PCA(:,1)'; e2=PCA(:,2)' ;e3=PCA(:,3)'; % 3 principal vector(3 eigenvector) of "data"
n1=[1 0 0] ; n2=[0 1 0] ; n3=[0 0 1]; % 3 unit vector Ox,Oy,Oz
%[PCA,~,lamda] = pca(data);%use this syntax to find eigenvalue of data. lamda is eigenvalue corresponding to 3 eigenvector (e1,e2,e3)
% --------Conclusion1: each eigvector(each Principal component)is unit vector(e1,e2,e3) ---------------------%
% --------Conclusion2: each eigvector(each Principal component) is always perpendicular to each other--------%
% --------It means that "dot product" or "scalar product" of each eigvector is equal to zero-----------------%
% --------Using the below syntax to find "scalar product" of 2 vector----------------------------------------%
% dot(e1,e2); % dot(e1,e3); % dot(e2,e3);
% --------Conclusion3: norm(||..x..||)of each eigvector(e1,e2,e3) is always equal to one-----%
% --------Using the below syntax to find "norm" of each eigvector----------------------------%
% norm(e1); % norm(e2); % norm(e3);
%% transformation matrix from "e" space to "n" space
R=[e1;e2;e3]; % rotation matrix. % match with xyz (e1//n1, e2//n2, e3//n3)
% R=[e3;e2;e1]; % If e1//n3, e2//n2, e3//n1
%%
newdata=(R*data')';
figure(2); n = fitNormal(newdata,1); grid on;
% jp.mathworks.c...
% create the input data: data = rand(50,3)*100; data(:,3) = data(:,1) * .4 + data(:,2) * .6 + rand(50,1);
------------------------------------GENERAL CASE-----------------------------------------
------------------------------%%%%%%%%%%%%-----------------------------------------
%% Load input 3D data
clear;clc;filename = 'test.txt'; inputfile = importfile(filename);
P = inputfile(:,1:3);%get data=coordinate(x,y,z) from set of data
x = P(:,1) ; y = P(:,2) ;z = P(:,3) ; % get (x,y,z) coordinate
x0 = x-mean(x) ; y0 = y-mean(y) ; z0 = z-mean(z) ; % remove mean
P1 = [x0 y0 z0] ; %this step to bring the coord of P near to the origin. The new coord will be created
scatter3(P1(:,1),P1(:,2),P1(:,3),'b.');%plot new coord of P1
HA=[min(P1(:,1)) min(P1(:,2)) max(P1(:,3))+1];%just for better visualaztion
hold on;scatter3(HA(:,1),HA(:,2),HA(:,3),'g.');%just for better visualaztion
%% Finding principal vector of 3D data P
PCA=pca(P);
e1=PCA(:,1)'; e2=PCA(:,2)' ;e3=PCA(:,3)'; % 3 principal vector(3 eigenvector) of "input data"
n1=[1 0 0] ; n2=[0 1 0] ; n3=[0 0 1]; % 3 unit vector Ox,Oy,Oz
%% transformation matrix from "e" space to "n" space
R=[e2;e1;e3]; % rotation matrix , match with xyz (e1//n2, e2//n1, e3//n3)
% R=[e1;e2;e3]; % rotation matrix , match with xyz (e1//n1, e2//n2, e3//n3)
% R=[e3;e2;e1]; % If e1//n3, e2//n2, e3//n1
%% Finding the new rotate data
newdata1=(R*P1')';%new data corresponding to P1 coordinate
hold on; scatter3(newdata1(:,1),newdata1(:,2),newdata1(:,3),'r.');
newdata=[newdata1(:,1)+mean(x),newdata1(:,2)+mean(y),newdata1(:,3)+mean(z)];
%% Plot the original & rotation 3D data
figure;scatter3(P(:,1),P(:,2),P(:,3),'b.');
hold on;scatter3(newdata(:,1),newdata(:,2),newdata(:,3),'r.');
HA=[min(P(:,1)) min(P(:,2)) max(P(:,3))+1];%just for better visualaztion
hold on;scatter3(HA(:,1),HA(:,2),HA(:,3),'g.');%just for better visualaztion
legend('original data(P)','rotated data(newdata)');title({'Plot the rotation matrix 3D point data';'(FINAL RESULT)'});
--------------------------------------------------------------------------------------
See more detail in below web to understand the formula of covariance:
www.itl.nist.g...
22 авг 2024