Arseny Grinchenko
Arseny Grinchenko

Reputation: 29

Looping in optimization

I have a dataset of 20 points which I need to enclose in an ellipse. I am trying to do this calculating a biggest distance between the center of ellipse and removing the farthest point, reconstructing a new ellipse and repeating the process again.

Here is the code:

P = [1.397100 0.934550
-0.708828 -3.993403
-0.775017 -0.167091
1.861729 -0.334958
-0.376357 -3.187580
0.294908 -0.765351
0.952188 -1.872313
0.524652 2.972442
0.889532 -0.331162
0.991093 0.278271
0.262071 0.078590
0.901017 0.320209
-0.797258 0.518452
-0.656796 0.268351
0.333667 0.601893
0.762157 0.613208
0.292147 -1.555187
0.122875 -0.860661
0.702863 -3.195442
-1.140430 -1.919686]'
t = 0.001;
K = convhulln(P');  
K = unique(K(:));  
Q = P(:,K);

[A , C] = MinVolEllipse(Q, t)
figure
plot(P(1,:),P(2,:),'*')
hold on
Ellipse_plot(A,C)

%Rule=size(P',1)

W=P'
v=C'
Cx=v(1)
Cy=v(2)
dist=sqrt((W(:,1)-Cx).^2+(W(:,2)-Cy).^2)
Remove=find(dist==max(dist(:)))
W(Remove,:)=[]
W=W'
Rule=size(W',1)
while Rule>5
    W=W';
    v=C';
    Cx=v(1);
    Cy=v(2);
    dist=sqrt((W(:,1)-Cx).^2+(W(:,2)-Cy).^2);
    Remove=find(dist==max(dist(:)));
    W(Remove,:)=[];
    W=W';
end
[A , C] = MinVolEllipse(W, t)
figure
plot(W(1,:),W(2,:),'*')
hold on
Ellipse_plot(A,C)

I understand that I did something wrong but now I'm stuck with "Busy" for about 10 minutes. How to properly loop the process?

Thanks!

Upvotes: 0

Views: 50

Answers (1)

mpaskov
mpaskov

Reputation: 1264

You need to update the value of Rule

while Rule>5
    W=W';
    v=C';
    Cx=v(1);
    Cy=v(2);
    dist=sqrt((W(:,1)-Cx).^2+(W(:,2)-Cy).^2);
    Remove=find(dist==max(dist(:)));
    W(Remove,:)=[];
    W=W';
    Rule=size(W',1)
end

As a separate point, you perform some redundant operation e.g. the first and the last line of you loop do the inverse operation. Maybe something like this: (Note the code is not tested)

while size(W,1)>5
    dist=sqrt((W(:,1)-C(1)).^2+(W(:,2)-C(2)).^2);
    % the second returned variable is the location
    [~, Remove] = max(dist(:));
    W(Remove,:)=[];
end

Upvotes: 2

Related Questions