-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsortem.m
31 lines (28 loc) · 832 Bytes
/
sortem.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function [P2,D2]=sortem(P,D)
% this function takes in two matrices P and D, presumably the output
% from Matlab's eig function, and then sorts the columns of P to
% match the sorted columns of D (going from largest to smallest)
%
% EXAMPLE:
%
% D =
% -90 0 0
% 0 -30 0
% 0 0 -60
% P =
% 1 2 3
% 1 2 3
% 1 2 3
%
% [P,D]=sortem(P,D)
% P =
% 2 3 1
% 2 3 1
% 2 3 1
% D =
% -30 0 0
% 0 -60 0
% 0 0 -90
D2=diag(sort(diag(D),'descend')); % make diagonal matrix out of sorted diagonal values of input D
[c, ind]=sort(diag(D),'descend'); % store the indices of which columns the sorted eigenvalues come from
P2=P(:,ind); % arrange the columns in this order