Convert values upper triangular matrix into vector

129 vues (au cours des 30 derniers jours)
Tim Elbers
Tim Elbers le 14 Mai 2019
Modifié(e) : Witold Waldman il y a environ 5 heures
Hi all,
Imagine a matrix of the following form:
1 2 3 4 5
0 4 6 8 10
0 0 9 12 15
0 0 0 16 20
0 0 0 0 25
Now i do like to obtain a vector with only the values of the upper triangular matrix. in other words a vector of underneath form.
[1 2 3 4 5 4 6 8 10 9 12 15 16 20 25]
What is the best way to obtain this vector?
Thanks in advance
  2 commentaires
Fathima Bareeda
Fathima Bareeda le 5 Juin 2021
What is the best way to obtain vector in the form of diagonal elements first and then upper triangular elements..
For eg:[1 4 9 16 25 2 3 4 5 6 8 10 12 15 20]
Walter Roberson
Walter Roberson le 5 Juin 2021
M = [ 1 2 3 4 5
0 4 6 8 10
0 0 9 12 15
0 0 0 16 20
0 0 0 0 25
]
M = 5×5
1 2 3 4 5 0 4 6 8 10 0 0 9 12 15 0 0 0 16 20 0 0 0 0 25
D = diag(M);
eg = [D.', squareform((M-diag(D)).')]
eg = 1×15
1 4 9 16 25 2 3 4 5 6 8 10 12 15 20

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 14 Mai 2019
Modifié(e) : Jan le 17 Mai 2019
At = A.';
m = tril(true(size(At)));
v = At(m).'
Maybe this is faster (>=R2016b due to auto-expanding):
At = A.';
m = (1:size(At,1)).' >= (1:size(At,2));
v = At(m);
  4 commentaires
Lucas Bezerra
Lucas Bezerra le 25 Mai 2021
It becomes even simpler if you use the triu function instead:
m = triu(true(size(A)));
v = A(m)
Jan
Jan le 25 Mai 2021
@Lucas Bezerra: The replies the elements in the wrong order [1 2 4 3 6 9 4 8 12 16 5 10 15 20 25].' instead of the wanted [1 2 3 4 5 4 6 8 10 9 12 15 16 20 25] .

Connectez-vous pour commenter.

Plus de réponses (2)

gb
gb le 30 Juil 2022
Modifié(e) : gb le 30 Juil 2022
A = [1 , 2, 3, 4, 5;
0, 4 , 6 , 8 , 10;
0, 0 , 9 , 12 , 15;
0, 0 , 0 , 16 , 20;
0, 0 , 0 , 0 , 25;]
A = 5×5
1 2 3 4 5 0 4 6 8 10 0 0 9 12 15 0 0 0 16 20 0 0 0 0 25
At = A';
m = tril(true(size(A)));
v = At(m)
v = 15×1
1 2 3 4 5 4 6 8 10 9
  1 commentaire
Jan
Jan le 30 Juil 2022
Why do you repeat this part of my answer?

Connectez-vous pour commenter.


Witold Waldman
Witold Waldman il y a environ 5 heures
Modifié(e) : Witold Waldman il y a environ 5 heures
A=[
1 2 3 4 5
0 4 6 8 10
0 0 9 12 15
0 0 0 16 20
0 0 0 0 25
];
% The following previously submitted code creates two intermediate
% matrices, At and m, in addition to the solution array v.
tic
At = A.';
m = tril(true(size(At)));
v = At(m).';
toc
Elapsed time is 0.003788 seconds.
v
v = 1x15
1 2 3 4 5 4 6 8 10 9 12 15 16 20 25
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% The following code does not create any intermediate matrices.
% It might be preferred when dealing with very large matrices.
% In testing, this code was the slower of the two.
tic
nA = size(A,1);
ibeg = 1;
for n = 1:nA
iend = ibeg+nA-n;
v(ibeg:iend) = A(n,n:nA);
ibeg = iend+1;
end
toc
Elapsed time is 0.002825 seconds.
v
v = 1x15
1 2 3 4 5 4 6 8 10 9 12 15 16 20 25
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by