flip half of matrix over the diagonal to make a symmetric matrix
112 ビュー (過去 30 日間)
古いコメントを表示
Dear all, If I have a half of a matrix, e.g
1
2 3
4 5 6
7 8 9 10
...
I want to flip it over the diagonal to make a symmetric matrix:
1 2 4 7
2 3 5 8
4 5 6 9
7 8 9 10
Please help. Thanks
2 件のコメント
Jan
2016 年 5 月 4 日
Tghe solution depends on how the triangular "array" is stored. Are there zeros in the upper right elements?
John D'Errico
2016 年 5 月 4 日
Is the matrix stored as a matrix, so only the lower triangle, with zeros as the upper triangle. Or is there junk in the upper triangle? Or do you have the elements of the lower triangle, stored in a vector?
All of these things are pertinent to any efficient solution.
採用された回答
Azzi Abdelmalek
2016 年 5 月 4 日
A=[1 0 0 0
2 3 0 0
4 5 6 0
7 8 9 10]
[n,m]=size(A);
B=A'+A
B(1:n+1:end)=diag(A)
3 件のコメント
Bill Tubbs
2020 年 5 月 28 日
I think you can do it in one line like this:
B = triu(A.',1) + tril(A) % Takes bottom half of A to make B symmetric
Also, this does not do a conjugate transpose.
その他の回答 (4 件)
Simon Liljestrand
2017 年 9 月 29 日
A=[1 0 0 0
2 3 0 0
4 5 6 0
7 8 9 10];
B=A'+triu(A',1)';
Ben McSeveney
2018 年 2 月 15 日
編集済み: Stephen23
2018 年 2 月 15 日
If I have a column vector e.g.
1
2
3
How do I quickly create a symmetric matrix i.e.
[1 2 3;
2 1 2;
3 2 1]
?
3 件のコメント
Jos (10584)
2018 年 2 月 15 日
for which the answer will be toeplitz
v = 1:5
toeplitz(v)
Tom Davis
2018 年 2 月 15 日
[a,circshift(a,1),circshift(a,2)]
triu(a' - a + ones(size(a,1))) + tril(a - a' + ones(size(a,1))) - eye(size(a,1))
Rohit Sachdeva
2024 年 4 月 9 日
編集済み: Rohit Sachdeva
2024 年 4 月 9 日
As most people have pointed out, I just wanted to add another way of doing this:
B = (A+A') - diag(diag(A));
The (A+A') part is clear to most of us. This is how the 2nd term works:
- First diag(.) extracts the diagonal elements of A.
- The next diag(.) creats a matrix with just those diagonal elements.
- Finally we subtract that matrix of diagonal elements from the (A+A') as required.
This eliminates the need of the eye(.) function. Hope it helps!
1 件のコメント
Steven Lord
2024 年 4 月 9 日
In general, this doesn't work.
A = magic(4)
B = (A+A') - diag(diag(A))
It does work if the matrix is real and one of the triangular parts already contains all 0 values.
C = triu(A)
D = (C+C') - diag(diag(C))
It doesn't work if the matrix is complex even if the matrix is triangular.
format shortg
C(1, 2) = 2+3i
D = (C+C') - diag(diag(C))
D is not symmetric, it is however Hermitian.
issymmetric(D)
ishermitian(D)
But if you used the non-conjugate transpose then the result is symmetric but not Hermitian:
E = (C+C.')-diag(diag(C))
issymmetric(E)
ishermitian(E)
参考
カテゴリ
Help Center および File Exchange で Operating on Diagonal Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!