Q:

How to find principal eigen direction?

I am trying to generate points within ellipsoid and then trying to fit with smooth ellipsoid surfaces. The goal is to fit with unknown data where i have to find value of a,b and c in 3 principal axes. 
Rinv should be equivalent to pc. But what i am getting pc in different order. So i have to find correct order to rotate my data to matlab coordinate. 
a=3;
b=5;
c=1;
index=1;
for i=1:500000
    x=10*rand-5;
    y=10*rand-5;
    z=10*rand-5;
    if ((x^2/a^2) + (y^2/b^2) + (z^2/c^2) -1) <0
        C(index,:)=[x,y,z];
        index=index+1;
    end
end

theta=pi/4; phi=pi/6; omega = pi/3;
Rx= [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Ry= [cos(phi) 0 sin(phi); 0 1 0; -sin(phi) 0 cos(phi)];
Rz= [cos(omega) -sin(omega) 0; sin(omega) cos(omega) 0; 0 0 1];
R= Rz*Ry*Rx; 
Rinv = inv(R);
X = C*R;
[pc,val]=eig(X'*X); E=diag(val);
[sa,sb]= sort(pc*E, 'descend');   sb 
order = [2,3,1];
nC= X*pc(:,order);
plot3(nC(:,1),nC(:,2),nC(:,3),'.')
hold on 
[x, y, z] = ellipsoid(0,0,0,a,b,c,30);
hSurface=surf(x, y, z, 'FaceColor','blue','EdgeColor','none');
alpha(0.5) 

Specially in this line nC= X*pc(:,order);
I am finding order manually. Can any one tells (1) how to find pc order correctly. (2) Value of a,b,c for unknown data set here "[x, y, z] = ellipsoid(0,0,0,a,b,c,30)"
Thanks  

A:

To compute the values of a,b,c from the eigen-space, you just need to compute the radius of the eigen-space projected ellipsoid. This is trivial because the ellipsoid is axis-oriented in eigen-space (which I assume is the only reason you are doing this in the first place). Just get the max and min in each direction!
nC= X*pc;
plot3(nC(:,1),nC(:,2),nC(:,3),'r.')
hold on 

a2=(max(nC(:,1))-min(nC(:,1)))/2;
b2=(max(nC(:,2))-min(nC(:,2)))/2;
c2=(max(nC(:,3))-min(nC(:,3)))/2;

[x, y, z] = ellipsoid(0,0,0,a2,b2,c2,30);
hSurface=surf(x, y, z, 'FaceColor','blue','EdgeColor','none');
alpha(0.5) 
axis equal

Note that this code actually makes order obsolete.

