61 lines
1.5 KiB
Matlab
61 lines
1.5 KiB
Matlab
function covareigen3( x, y, z )
|
|
% computes covariance matrix from the triples x, y, z
|
|
% diagonalizes covariance matrix
|
|
% x, y and z are column vectors
|
|
|
|
% covariance matrix:
|
|
cv = cov( [ x y z ] );
|
|
|
|
% eigen values:
|
|
[ v , d] = eig( cv )
|
|
|
|
% plots:
|
|
subplot( 1, 2, 1 );
|
|
hold on;
|
|
|
|
% scatter plot:
|
|
view( 3 );
|
|
scatter3( x, y, z, 0.1, 'b', 'filled', 'MarkerEdgeColor', 'blue' );
|
|
xlabel( 'x' );
|
|
ylabel( 'y' );
|
|
zlabel( 'z' );
|
|
grid on;
|
|
|
|
% plot eigenvectors:
|
|
quiver3( ones( 1, 3 ).*mean( x ), ones( 1, 3 )*mean(y), ones( 1, 3 )*mean(z), v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', v(3,:).*sqrt(diag(d))', 'r', 'LineWidth', 3, 'AutoScale', 'off', 'AutoScaleFactor', 1.0, 'MaxHeadSize', 0.7 )
|
|
|
|
%axis( 'equal' );
|
|
hold off;
|
|
|
|
% 2D scatter plots:
|
|
subplot( 2, 6, 4 );
|
|
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
|
xlabel( 'x' )
|
|
ylabel( 'y' )
|
|
subplot( 2, 6, 5 );
|
|
scatter( x, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
|
xlabel( 'x' )
|
|
ylabel( 'z' )
|
|
subplot( 2, 6, 6 );
|
|
scatter( y, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
|
xlabel( 'y' )
|
|
ylabel( 'z' )
|
|
|
|
% sort the eigenvalues:
|
|
[d,inx] = sort( diag(d), 'descend' );
|
|
|
|
subplot( 2, 2, 4 );
|
|
hold on;
|
|
% subtract means:
|
|
x = x - mean( x );
|
|
y = y - mean( y );
|
|
% project onto eigenvectors:
|
|
nx = [ x y z ] * v(:,inx);
|
|
scatter( nx(:,1), nx(:,2), 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
|
xlabel( 'ex' )
|
|
ylabel( 'ey' )
|
|
axis( 'equal' );
|
|
hold off;
|
|
|
|
end
|