How to fit largest ellipsoid for 3d data points such that it covers all points?
5 views (last 30 days)
Show older comments
Hi, I have search a lot for ellipsoid fit to 3d data and come up with some answer but I want some improvement in my method such that it covers all the data points. My code is here.
Y=data; % data in non-principle coordinate
X = data;
oldmu =mean(X);
X= bsxfun(@minus,X,oldmu); % mean-centered data
[pc val] = eig(X'*X );
nC = X*pc; % data in principle coordinate
a2=(max(nC(:,1))-min(nC(:,1)))/2; %range of data in X
b2=(max(nC(:,2))-min(nC(:,2)))/2; %range of data in Y
c2=(max(nC(:,3))-min(nC(:,3)))/2; %range of data in Z
[x, y, z] = ellipsoid(0,0,0,a2,b2,c2,40);
% fit ellipsoid in principle coordinate
tt=[x(:) y(:) z(:)]*inv(pc);%ellipsoid in non-principle coordinate
nx=reshape(tt(:,1),size(x,1),size(x,2));
ny=reshape(tt(:,2),size(x,1),size(x,2));
nz=reshape(tt(:,3),size(x,1),size(x,2));
% plot ellipsoid in non-principle coordinate
hSurface=surf(nx+oldmu(1), ny+oldmu(2), nz+oldmu(3), 'FaceColor','r','EdgeColor','none','FaceAlpha',0.6);
hold on
%plot data in non-principle coordinate
plot3(Y(:,1),Y(:,2),Y(:,3),'k.-')
end
The data file is here.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/166564/image.jpeg)
1 Comment
Baptiste Ottino
on 7 Aug 2017
What do you mean by "covers all the data points". Do you want an ellipsoid that encompasses all the data points? Or is all your data located on the surface of an ellipsoid for sure?
Answers (1)
Image Analyst
on 8 Aug 2017
I think it's pretty difficult. John did the same thing in 2-D here http://www.mathworks.com/matlabcentral/fileexchange/34767-a-suite-of-minimal-bounding-objects If it were easy he'd probably have done it already.
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!