フィルターのクリア

Mirror pattern of the leading diagonal about the middle element of the matrix

2 ビュー (過去 30 日間)
Hi All,
I am interested to create the following matrix but unable to think about the logic to create it.
for example : for n = 5 ; the elememts of leading diagonal are 2 1 0 1 2 and rest elements 0
for n = 7, the elements of the leading diagonal are 3 2 1 0 1 2 3 and rest elements are 0.
Any help will be highly appreciated.

採用された回答

Star Strider
Star Strider 2021 年 1 月 2 日
This works for odd ‘n’:
dv = @(n) abs(diag(unique(fix(-n/2:n/2)))); % Create Diagonal Matrix
n = 5;
m5 = dv(n)
n = 7;
m7 = dv(n)
producing:
m5 =
2 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 1 0
0 0 0 0 2
m7 =
3 0 0 0 0 0 0
0 2 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 2 0
0 0 0 0 0 0 3
.
  6 件のコメント
Swati Sarangi
Swati Sarangi 2021 年 1 月 5 日
wow! You're a Genius!
Star Strider
Star Strider 2021 年 1 月 5 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by