Change of basis, rotating an ellipse

15 views (last 30 days)
I though I knew how to do this, apperently i did not. I'm supposted to draw a confidence ellips aroud a cluster of data centered aroud the mean of said cluster. I've figured how to draw the ellipse, now i need to rotate it. I would like to change the basis that I'm working with so that the mean of the cluster becomes my new basis, I'm thinking that would solve the rotational issue? Can anyone help?
data=[191 197 208 180 180 188 210 196 191 179 202 200 192 199 186 197 201 190 209 187 207 178 202 ...
205 190 189 211 216 189 173 194 198 180 190 191 196 207 209 179 186 174 181 189 189;
284 285 273 275 280 283 288 271 257 289 285 272 282 280 266 285 295 282 305 285 297 268 271 285 ...
280 277 310 305 274 271 280 300 272 292 286 285 286 303 261 262 245 250 262 258]';
x=mean(data)'; %% Mean, and what I would like my new basis to be
S=cov(data); lambda=svd(S); n=length(data); p=size(data,2); F=3.21;
major=2*sqrt(lambda(1))*sqrt(p*(n-1)*F/(n*(n-p))); %% Major axis
minor=2*sqrt(lambda(2))*sqrt(p*(n-1)*F/(n*(n-p))); %% Minor axis
th=linspace(0,2*pi);
X=[x(1)+major*cos(th); x(2)+minor*sin(th)];
angle=atan(x(1)/x(2)); %% rotational angle
R=[cos(angle) -sin(angle); sin(angle) cos(angle)]; %% Rotational matrix, should i muliply with new basis from left?
CE1=X; CE1=CE1'; %% Ellipse (blue), not rotated
CE2=R*X; CE2=CE2'; %% Ellipse (red), rotated, but not in a correct way
figure()
hold on
plot(CE1(:,1),CE1(:,2)), %axis equal
plot(CE2(:,1),CE2(:,2))
plot(data(:,1),data(:,2),'bo')

Accepted Answer

Image Analyst
Image Analyst on 29 Sep 2021
Edited: Image Analyst on 29 Sep 2021
Looks like you forgot to subtract the center when you rotated so it's rotating around the origin, not the center of your ellipse.
You'd need to
  1. subtract the center to put it at the origin
  2. rotate the origin-centered ellipse
  3. add the center back in to translate it to its final location.
See attached demos where I rotate ellipses. Basically you multiply the N-by-2 (x,y) points array by the rotation matrix.
  2 Comments
John D'Errico
John D'Errico on 29 Sep 2021
Edited: John D'Errico on 29 Sep 2021
"Basically you multiply the N-by-2 (x,y) points array by the rotation matrix."
Um, not exactly. That ony applies when the ellipse center is at the origin. So if you have already subtracted off the mean, yes. But then you need to translate back for the mean.
Image Analyst
Image Analyst on 29 Sep 2021
Correct John. I think you were posting when I was editing and reposting, which I needed to do after I saw where the ellipse in his attempt ended up.

Sign in to comment.

More Answers (0)

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!