How to shift the element in diagonal matrix

As question mentioned, I created a matrix
v = [4 5 6 7]
diag(v)
then, I would like to have a matrix as [0 4 0 0; 0 0 5 0; 0 0 0 6; 7 0 0 0] also [0 0 0 4; 5 0 0 0; 0 6 0 0; 0 0 7 0] by using the command diag, is there any way to do?
Thank you !

 採用された回答

Birdman
Birdman 2018 年 3 月 24 日

0 投票

>>circshift(diag(v),1,2)
0 4 0 0
0 0 5 0
0 0 0 6
7 0 0 0
>>circshift(diag(v),-1,2)
0 0 0 4
5 0 0 0
0 6 0 0
0 0 7 0

1 件のコメント

jia ying
jia ying 2018 年 3 月 24 日
Thanks a lot, your help is appreciated !
*ps: new to matlab

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOperating on Diagonal Matrices についてさらに検索

質問済み:

2018 年 3 月 24 日

コメント済み:

2018 年 3 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by