フィルターのクリア

how can i write function to display matrix?

2 ビュー (過去 30 日間)
hassan elkholy
hassan elkholy 2020 年 12 月 23 日
回答済み: Masoud Dorvash 2021 年 1 月 18 日
how can i write function to display a zero matrix and its diagonal calculated by function
f(n) = f(n-1)+2f(n-2)+3
it has only one input n
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 12 月 23 日
If you start with an all-zero matrix then every element of the matrix will be the same for any given n, so it seems odd to talk about the diagonal.
You would need a matrix multiplication step to perturb the elements so that the diagonals were not all the same.

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

回答 (1 件)

Masoud Dorvash
Masoud Dorvash 2021 年 1 月 18 日
Hello,
You just need to use
D = diag(v);
This command will create a matrix with the elements of v on the main diagonal.
Try to first create your v vector (here in your example is f(n)) then put it there.
v = [1 6 2 14 1 8 46];
diag(v)
ans =
1 0 0 0 0 0 0
0 6 0 0 0 0 0
0 0 2 0 0 0 0
0 0 0 14 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 8 0
0 0 0 0 0 0 46

カテゴリ

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