Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Sorting for EIG & PCA

Subject: Sorting for EIG & PCA

From: Nehal

Date: 3 Nov, 2010 03:44:04

Message: 1 of 3

[V,D] = eig(A);
from the above command, I have got
V =
0.363 -0.623 0.286 -0.086 0.070
0.037 -0.003 -0.003 0.003 -0.013
0.002 0.002 -0.002 -0.001 0.005
0.001 0.002 -0.002 -0.000 0.001
-0.001 0.002 0.001 -0.002 -0.005

and D =
44.2973512368316 0 0 0 0
0 52.6649974049839 0 0 0
0 0 71.1476465777435 0 0
0 0 0 117.212343045057 0
0 0 0 0 132.373288597066

where A is my image arrays.
Now I want to sort D in descending order. After the sorting, according to the new sorted D's positions, I want to sort V too. Finally I need the sorted V but that should be according to sorted D's order.

I need this for both eig and pca.
could anyone help me..?

Subject: Sorting for EIG & PCA

From: Roger Stafford

Date: 3 Nov, 2010 04:37:04

Message: 2 of 3

"Nehal " <arnab620@yahoo.com> wrote in message <iaqlq4$a1s$1@fred.mathworks.com>...
> [V,D] = eig(A);
> from the above command, I have got
> V =
> 0.363 -0.623 0.286 -0.086 0.070
> 0.037 -0.003 -0.003 0.003 -0.013
> 0.002 0.002 -0.002 -0.001 0.005
> 0.001 0.002 -0.002 -0.000 0.001
> -0.001 0.002 0.001 -0.002 -0.005
>
> and D =
> 44.2973512368316 0 0 0 0
> 0 52.6649974049839 0 0 0
> 0 0 71.1476465777435 0 0
> 0 0 0 117.212343045057 0
> 0 0 0 0 132.373288597066
>
> where A is my image arrays.
> Now I want to sort D in descending order. After the sorting, according to the new sorted D's positions, I want to sort V too. Finally I need the sorted V but that should be according to sorted D's order.
>
> I need this for both eig and pca.
> could anyone help me..?
- - - - - - - - - -
  For the eigenvalues and eigenvectors,

 [D2,p] = sort(diag(D),'descend');
 V2 = V(:,p);
 D2 = diag(D2);

  If your eigenvalues are complex, which they could easily be, this sorting may not be what you want.

Roger Stafford

Subject: Sorting for EIG & PCA

From: Greg Heath

Date: 3 Nov, 2010 07:29:48

Message: 3 of 3

On Nov 2, 11:44 pm, "Nehal " <arnab...@yahoo.com> wrote:
> [V,D] = eig(A);
> from the above command, I have got
> V =
> 0.363    -0.623    0.286    -0.086    0.070
> 0.037    -0.003    -0.003    0.003    -0.013
> 0.002    0.002    -0.002    -0.001    0.005
> 0.001    0.002    -0.002    -0.000    0.001
> -0.001    0.002    0.001    -0.002    -0.005
>
> and D =
> 44.2973512368316        0       0       0       0
> 0       52.6649974049839        0       0       0
> 0       0       71.1476465777435        0       0
> 0       0       0       117.212343045057        0
> 0       0       0       0       132.373288597066
>
> where A is my image arrays.
> Now I want to sort D in descending order. After the sorting, according to the new sorted D's positions, I want to sort V too. Finally I need the sorted V but that should be according to sorted D's order.
>
> I need this for both eig and pca.
> could anyone help me..?

Something is wrong:

close all, clear all, clc

V = [
0.363 -0.623 0.286 -0.086 0.070
0.037 -0.003 -0.003 0.003 -0.013
0.002 0.002 -0.002 -0.001 0.005
0.001 0.002 -0.002 -0.000 0.001
-0.001 0.002 0.001 -0.002 -0.005 ]

D = [
44.2973512368316 0 0 0 0
0 52.6649974049839 0 0 0
0 0 71.1476465777435 0 0
0 0 0 117.212343045057 0
0 0 0 0 132.373288597066 ]

%V is not unitary:

>> VVp = V*V'

VVp =
      0.61399 0.013274 -0.000656 -0.001385 -0.001501
     0.013274 0.001565 6e-006 2.4e-005 1.3e-005
    -0.000656 6e-006 3.8e-005 1.5e-005 -2.3e-005
    -0.001385 2.4e-005 1.5e-005 1e-005 -4e-006
    -0.001501 1.3e-005 -2.3e-005 -4e-006 3.5e-005

% So, let's start over

>> A = V*D*V'
[U L] = eig(A)
UUp = U*U'

A =
       33.613 0.48164 -0.017746 -0.080971 -0.087522
      0.48164 0.085183 -0.005567 2.9045e-005 0.0057326
    -0.017746 -0.005567 0.004099 0.0012457 -0.0030951
    -0.080971 2.9045e-005 0.0012457 0.00067192 -0.0006378
    -0.087522 0.0057326 -0.0030951 -0.0006378 0.0041043

U =
     -0.99989 -0.014017 0.0022706 0.0031656 -0.0027242
     -0.01436 0.99257 -0.11114 -0.028868 0.037658
   0.00053008 -0.073818 -0.71536 0.62135 0.31102
    0.0024081 0.012855 -0.25482 0.18336 -0.94936
    0.0026008 0.094857 0.64107 0.76123 -0.023754

L =
        33.62 0 0 0 0
            0 0.079344 0 0 0
            0 0 0.0065079 0 0
            0 0 0 0.00084291 0
            0 0 0 0 1.4356e-005

% Descending order!

UUp =
            1 -1.0219e-017 -3.0358e-018 -4.3368e-018 1.7483e-018
 -1.0219e-017 1 3.6429e-017 2.7756e-017 -1.0701e-016
 -3.0358e-018 3.6429e-017 1 -6.1062e-016 5.2736e-016
 -4.3368e-018 2.7756e-017 -6.1062e-016 1 -1.0894e-015
  1.7483e-018 -1.0701e-016 5.2736e-016 -1.0894e-015 1

% Unitary!
% In addition

>> [U2 L2] = eigs(A);
max(max(abs(U-U2)))
max(max(abs(L-L2)))

ans = 0
ans = 0

I have MATLAB 6.5, maybe eig and/or eigs has
changed since then.

Hope this helps.

Greg

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us