Why are the eigenvalues of eig() sorted in ascending order? - matlab

I'm trying to find eigenvalues of a matrix with eig.
I define the matrix with example data:
A = magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
and
D = eig(A,'matrix')
D =
65.0000 0 0 0 0
0 -21.2768 0 0 0
0 0 -13.1263 0 0
0 0 0 21.2768 0
0 0 0 0 13.1263
But if I use
C = cov(A)
and get eigenvalues from the covariance matrix, this is the result:
DC = eig(C,'matrix')
DC =
-0.0000 0 0 0 0
0 35.4072 0 0 0
0 0 44.9139 0 0
0 0 0 117.5861 0
0 0 0 0 127.0928
Why are the eigenvalues from the covariance matrix sorted in ascending order?

Sorting is merely a choice of convenience. There's no such thing as a 'real' position of an eigenvector, just as (x,y) is just as valid as (y,x). Since a lot of matrix techniques work on eigenvectors in order of decreasing eigenvalue (i.e. most important first), it makes sense to structure them accordingly.

Related

Retriveing the intersection of matrices

whos.exit whos condition1 result
650 452 1 0
654 456 0 0
254 650 1 1
785 412 1 0
756 654 1 1
744 0 0
125 1 0
985 1 0
... ... ...
I wish obtain the result matrix.
Result matrix contains all "whos" which satify the condition1 and are present whos.exit but in no particular order. Note: all elements in whos.exit are unique and the result of whos(condition1) will give unique whos.
You can use ismember -
result = ismember(whos,whos.exit).*condition1
Or bsxfun -
result = any(bsxfun(#eq,whos,whos.exit.'),2).*condition1
Since whos is an in-built command in MATLAB, I would suggest using some other variable name there as a matter of good practice.
You could use intersect
intersect(whos.exit,whos.*condition1)
ans =
650
654
Or if you want a binary array (not as elegant asismember though)
A=zeros(size(whos.exit,1),1);
[~,~,iwe]=intersect(whos,whos.exit);
A(iwe) = 1;
A.*c1
ans =
0
0
1
0
1
0
0
0
or
[~,~,iwe]=intersect(whos,whos.exit);
sum((((c1.*whos.exit)./whos.exit(iwe)')==1)')'
ans =
0
0
1
0
1
0
0
0
Details
Find the indices in whose.exit whose values are in both arrays.
[~,~,iwe]=intersect(whos,whos.exit)
iwe =
3
5
Find where those values are. I just use a division because a value divided by itself will show a 1 and that tells us where the values are. Each row represents the value(s) we are looking for and the column the location of this value. The first value (whos.exit(iwe(1))) is location at position 3 and the second (whos.exit(iwe(2))) is location at position 5.
(((c1.*whos.exit)./whos.exit(iwe)')==1)'
ans =
0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0
The we just sum and transpose that to get the binary array
sum((((c1.*whos.exit)./whos.exit(iwe)')==1)')'
ans =
0
0
1
0
1
0
0
0

Extended block diagonal matrix in Matlab

I know that to generate a block-diagonal matrix in Matlab the command blkdiag generates such a matrix:
Now I am faced with generating the same block-diagonal matrix, but with also matrix elements B_1, B_2,..., B_{n-1} on the upper diagonal, zeros elsewhere:
I guess this can be hardcoded with loops, but I would like to find a more elegant solution. Any ideas on how to implement such a thing?
P.S. I diag command, that using diag(A,k) returns the kth diagonal. I need something for writing in the matrix, for k>0, and for block matrices, not only elements.
There is a submission on the File Exchange that can do this:
(Block) tri-diagonal matrices.
You provide the function with three 3D-arrays, each layer of the 3D array represents a block of the main, sub- or superdiagonal. (Which means that the blocks will have to be of the same size.) The result will be a sparse matrix, so it should be rather efficient in terms of memory.
An example usage would be:
As = bsxfun(#times,ones(3),permute(1:3,[3,1,2]));
Bs = bsxfun(#times,ones(3),permute(10:11,[3,1,2]));
M = blktridiag(As, zeros(size(Bs)), Bs);
where full(M) gives you:
1 1 1 10 10 10 0 0 0
1 1 1 10 10 10 0 0 0
1 1 1 10 10 10 0 0 0
0 0 0 2 2 2 11 11 11
0 0 0 2 2 2 11 11 11
0 0 0 2 2 2 11 11 11
0 0 0 0 0 0 3 3 3
0 0 0 0 0 0 3 3 3
0 0 0 0 0 0 3 3 3
This could be one approach based on kron, tril & triu -
%// Take all A1, A2, A3, etc in a cell array for easy access and same for B
A = {A1,A2,A3,A4}
B = {B1,B2,B3}
%// Setup output array with the A blocks at main diagonal
out = blkdiag(A{:})
%// logical array with 1s at places where kth diagonal elements are to be put
idx = kron(triu(true(numel(A)),k) & tril(true(numel(A)),k),ones(size(A{1})))>0
%// Put kth diagonal blocks using the logical mask
out(idx) = [B{1:numel(A)-k}]
Sample run with k = 1 for 2 x 2 sizes matrices -
>> A{:}
ans =
0.3467 0.7966
0.6228 0.7459
ans =
0.1255 0.0252
0.8224 0.4144
ans =
0.7314 0.3673
0.7814 0.7449
ans =
0.8923 0.1296
0.2426 0.2251
>> B{:}
ans =
0.3500 0.9275
0.2871 0.0513
ans =
0.5927 0.8384
0.1629 0.1676
ans =
0.5022 0.3554
0.9993 0.0471
>> out
out =
0.3467 0.7966 0.3500 0.9275 0 0 0 0
0.6228 0.7459 0.2871 0.0513 0 0 0 0
0 0 0.1255 0.0252 0.5927 0.8384 0 0
0 0 0.8224 0.4144 0.1629 0.1676 0 0
0 0 0 0 0.7314 0.3673 0.5022 0.3554
0 0 0 0 0.7814 0.7449 0.9993 0.0471
0 0 0 0 0 0 0.8923 0.1296
0 0 0 0 0 0 0.2426 0.2251

Matlab:How to find the indices of rows without any zero in a matrix?

How to find the indices of rows without any zero in a matrix?
Example:
A = [
14 0 6 9 8 17
85 14 1 3 0 99
0 0 0 0 0 0
29 4 5 8 7 46
0 0 0 0 0 0
17 0 5 0 0 49
]
the desired result :
V =[4]
Since Adiel did not post an answer, I'll make their comment a CW: the command
V = find(all(A,2))
does the job, because all(A,2) processes every row, returning 1 if there are any nonzero entries. Then find returns the indices of nonzero entries, which are the desired row numbers.
Similarly, V = find(all(A,1)) works column-wise.

Reshape / Transform an upper triangular matrix in MATLAB

I have an upper triangular matrix (without the diagonal) given by:
M = [0 3 2 2 0 0; 0 0 8 6 3 2; 0 0 0 3 2 1; 0 0 0 0 2 1; 0 0 0 0 0 0]
The resulting matrix should look like this:
R = [0 0 0 0 0 0; 0 2 0 0 0 0; 2 3 1 0 0 0; 2 6 2 1 0 0; 3 8 3 2 0 0]
Since I couldn't find a simple explanation which describes my goal I tried to visualize it with an image:
I already tried lots of different combinations of rot90, transpose, flipud etc., but I could't find the right transformation that gives me the matrix R
EDIT:
The rows of the matrix M are not always sorted as in the example above. For another matrix M_2:
M_2 = [0 2 3 1 0 0; 0 0 3 6 3 9; 0 0 0 1 2 4; 0 0 0 0 2 6; 0 0 0 0 0 0]
the resulting matrix R_2 need to be the following:
R_2 = [0 0 0 0 0 0; 0 9 0 0 0 0; 1 3 4 0 0 0; 3 6 2 6 0 0; 2 3 1 2 0 0]
Again the visualization below:
EDIT:
Inspired by the tip from #Dan's comment, it can be further simplified to
R = reshape(rot90(M), size(M));
Original Answer:
This should be a simple way to do this
F = rot90(M);
R = F(reshape(1:numel(M), size(M)))
which returns
R =
0 0 0 0 0 0
0 2 0 0 0 0
2 3 1 0 0 0
2 6 2 1 0 0
3 8 3 2 0 0
The idea is that when you rotate the matrix you get
>> F = rot90(M)
F =
0 2 1 1 0
0 3 2 2 0
2 6 3 0 0
2 8 0 0 0
3 0 0 0 0
0 0 0 0 0
which is a 6 by 5 matrix. If you consider the linear indexing over F the corresponding indices are
>> reshape(1:30, size(F))
1 7 13 19 25
2 8 14 20 26
3 9 15 21 27
4 10 16 22 28
5 11 17 23 29
6 12 18 24 30
where elements 6, 11, 12, 16, 17, 18 , and ... are zero now if you reshape this to a 5 by 6 matrix you get
>> reshape(1:30, size(M))
1 6 11 16 21 26
2 7 12 17 22 27
3 8 13 18 23 28
4 9 14 19 24 29
5 10 15 20 25 30
Now those elements corresponding to zero values are on top, exactly what we wanted. So by passing this indexing array to F we get the desired R.
Without relying on order (just rotating the colored strips and pushing them to the bottom).
First solution: note that it doesn't work if there are zeros between the "data" values (for example, if M(1,3) is 0 in the example given). If there may be zeros please see second solution below:
[nRows nCols]= size(M);
R = [flipud(M(:,2:nCols).') zeros(nRows,1)];
[~, rowSubIndex] = sort(~~R);
index = sub2ind([nRows nCols],rowSubIndex,repmat(1:nCols,nRows,1));
R = R(index);
Second solution: works even if there are zeros within the data:
[nRows nCols]= size(M);
S = [flipud(M(:,2:nCols).') zeros(nRows,1)];
mask = 1 + fliplr(tril(NaN*ones(nRows, nCols)));
S = S .* mask;
[~, rowSubIndex] = sort(~isnan(S));
index = sub2ind([nRows nCols],rowSubIndex,repmat(1:nCols,nRows,1));
R = S(index);
R(isnan(R)) = 0;
Alternate option, using loops:
[nRows nCols]= size(M);
R = zeros(nRows,nCols);
for n = 1:nRows
R((n+1):nCols,n)=fliplr(M(n,(n+1):nCols));
end

Multiplication of Vectors with diagonal of a matrices from t to t+1 in Matlab

Still very new to programming...
I have 9x1 Vectors at time t, t+1, t+2 etc.
[10 10 10 10 10 10 10 10 10]'
and matrices. Each matrix is 9x9 and also at time 1, t+1, t+2 etc. =
1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1
They are 3d matrices and I want to make them 4d in the future.
I want to multiply vector(:,:,t) with the diagonal of matrix at time t and output vector(:,:,t+1).
So in short...
vector t multiplied by diag matrix t = vector t+1
vector t+1 multiplied by diag matrix t+1 = vector t+2
vector t+2 multiplied by diag matrix t+2 = vector t+3 ... and so on.
the diagonal numbers change in each time step but for simplicity sake, let's keep them all at 1 for the moment.
I've tried using diag but it states I have to use a 2D input so only works when I ignore t.
Cheers for your help guys - it's helping me learn a lot. Any hints or solutions will be much appreciated. I know you guys know the simplest and most efficient solutions.
Since the result of each step depends on the previous iteration, it cannot be vectorized. So I would go with #JohnColby's solution.
For what it's worth, here is an example how you would extract the diagonals of a 3D matrix in a vectorized way:
M = reshape(1:3*4*3,[3 4 3]);
[r,c,p] = size(M);
ind = bsxfun(#plus, (1:r+1:r*c)', (0:p-1).*r*c);
M(ind)
Each column of the result corresponds to the diagonal elements from each slice (doesn't have to be square matrix):
>> M
M(:,:,1) =
1 4 7 10
2 5 8 11
3 6 9 12
M(:,:,2) =
13 16 19 22
14 17 20 23
15 18 21 24
M(:,:,3) =
25 28 31 34
26 29 32 35
27 30 33 36
>> M(ind)
ans =
1 13 25
5 17 29
9 21 33
Here you go:
n = 10;
% Make sample data
t = zeros(9,1,n);
t(:,1,1) = 1;
T = repmat(diag(ones(9,1)), [1 1 n]);
% Loop though to fill in t based on previous t and T
for i = 2:n
t(:,1,i) = t(:,1,i-1) .* diag(T(:,:,i-1));
end
Now all of t should be 1.