function y=select(P)
%SELECT expresses a diagonalizable state
% matrix P in terms of
% lambda1, A1
% lambda2, A2, etc.
% If P has m distinct eigenvalues, SELECT returns
% m+1 matrices. The first matrix is the diagonal
% matrix containing all the eigenvalues.
% The other matrices are A1, A2, ..., Am.
m = size(P,1); %determine the number of rows, or columns.
[X,D] = eig(P); % determine eigenvectors and eigenvalues
X_inv = inv(X);
y(:,:,1) = D^n; % first matrix is D^n
for i=1:m,
sel = zeros(m);
sel(i,i) = 1; % sel is a matrix to deterine A_i
y(:,:,i+1) = X*sel*X_inv;
end