How to Finding Principle Axis and Determining the Specific Shape?

34 views (last 30 days)
I have the scattered data as shown in below:
I want to determine the principle axis of these scattered data,
and then I want to determine the specific rectangular in which the error(?) is minimized.
In other words, the sum of distances between the outer line of square and each point is minimum.
It seems that the matlab comand 'pca' partically supports it, but I don't know how to use it effectively.
How to realize this idea? Is there other useful matlab command? I eagerly await reply.

Accepted Answer

Matt J
Matt J on 3 Nov 2021
Edited: Matt J on 3 Nov 2021
The principle axes you can find with svd(),
XY=[X(:),Y(:)]; %the scattered data
mu=mean(XY);
P=svd(XY-mu,0); %P contains the principle axes
Once you've done the pca, you can rotate/translate your scattered (X,Y) data so that the principle axes are aligned with the coordinate axes and so that the data is centered at the origin.
xy=P.'*(XY-mu); %rotated data
Then, the problem boils down to estimating the dimensions a and b of a non-rotated rectangle. The latter, you might be able to do with fminsearch:
xp=abs(xy(:,1)); yp=abs(xy(:,2));
ab=fminsearch(@(ab) objective(ab,xp,yp), [max(xp),max(yp)] );
function fval = objective(ab,x, y)
a=ab(1)/2; b=ab(2)/2; %half length and width of rectangle
%%% Separate points into 4 regions (positive quadrant only)
in= x<a & y<b; %inside rectangle
out1= x>=a & y<b;
out2= x>=a & y>=b;
out3= x<a & y>=b;
%%% Distances of inner points to boundary
xin=x(in); y=y(in); %points inside
Din=sum( min([abs(xin-a), abs(xin+a), abs(yin-b), abs(yin+b)],[],2) );
%%% Distances of outer points to boundary
Dout1 = sum(x(out1)-a);
Dout2 = sum(vecnorm([x(out2),y(out2)] -[a,b],2,2));
Dout3 = sum(y(out3)-b);
fval=Din+Dout1+Dout2+Dout3;
end

More Answers (0)

Tags

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!