Symmetric matrix element repositioning

3 ビュー (過去 30 日間)
And Meng
And Meng 2017 年 1 月 19 日
コメント済み: Xuekun Meng 2017 年 1 月 20 日
Hi,
I have a symmetric matrix. I need to reposition the desired diagonal elements and their corresponding elements to the low/right side.
In the following code, the desired diagonal element is 7 and 8. Their diagonal element will be placed at the lower right side of the matrix, e.g.Y(7,7) will be placed at Ynew(8,8). As well as their relevant element, e.g. Y(1,7) will be placed at Ynew(1,8). Is there any way that I can do it more efficiently?
n=9;
%random symmetric matrix
Y=magic(n);
Y=(Y+Y')/2;
% Yrr
Yrr=[Y(7,7) Y(7,8);Y(8,7) Y(8,8)];
% Ytr
Ytr=[Y([1 2 3 4 5 6 9],7) Y([1 2 3 4 5 6 9],8)];
% Ytt
Ytt=[Y([1 2 3 4 5 6 9],[1 2 3 4 5 6 9])];
% Ytr
Yrt=Ytr.';
% new symmetric matrix
Ynew=[Ytt Ytr;Yrt Yrr];
Any advice would be much appreciated. Thanks for reading.

回答 (1 件)

Matt J
Matt J 2017 年 1 月 19 日
編集済み: Matt J 2017 年 1 月 19 日
I think this is what you want,
perm=[1 2 3 4 5 6 9 7 8];
Ynew=Y(perm,perm);
  1 件のコメント
Xuekun Meng
Xuekun Meng 2017 年 1 月 20 日
Got it! Thank you Matt!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by