how to put output of a function in diagonal of matrix

6 ビュー (過去 30 日間)
akay
akay 2016 年 7 月 29 日
回答済み: Walter Roberson 2016 年 7 月 31 日
how would i have a function output become the diagonal of a matrix, for instance
function y=f(x)
y= 3x+1;
end
how do i put the output of this function as the diagonal of a matrix with 0s everywhere else. i've attempted some type of for loop but it doesnt seem to work
i.e. the diagonal should have y(0), y(1), y(2),... y(n),
for different values of n

採用された回答

James Tursa
James Tursa 2016 年 7 月 29 日
編集済み: James Tursa 2016 年 7 月 29 日
If y is a vector, then simply
y = diag(y);
If y is a matrix and you are trying to pick off the diagonal, then e.g.,
y = diag(diag(y));
  2 件のコメント
akay
akay 2016 年 7 月 29 日
y is the function, i would like the output of this function to be the diagonal of the matrix, whereas you're already assuming y has been allocated the values of the function
Stephen23
Stephen23 2016 年 7 月 31 日
@akay: y is not a function, f if your function. The answer you have given puts y into the diagonal of a matrix. If this is not what you want please explain in more detail.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 7 月 31 日
y = diag(3*x+1);

カテゴリ

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