Defining formulas for diagonal and offdiagonal elements of a matrix

9 ビュー (過去 30 日間)
Jeong Ho
Jeong Ho 2015 年 4 月 2 日
コメント済み: Jeong Ho 2015 年 4 月 2 日
I have a vector M. I want to create a matrix s.t.
A(j,j) = M(j)*(1 + (M(j)-1)*roh_w)
A(j,k) = M(j)*M(k)*roh_b, whenever j!=k.
I can do
A = diag(M.*(1+(M-1)*roh_w) - M.^2*roh_b);
A = A + M.'*M*roh_b;
i.e., I first create a diagonal matrix s.t.
A(j,j) = M(j)*(1 + (M(j)-1)*roh_w) - M(j)*M(k)*roh_b,
I then add a matrix whose effect is canceled out by the second term right above for diagonal elements. Is there a prettier way of doing this?
Quintessentially, is there a one-liner code s.t. you give formula for diagonal elements and nondiagonal elements, e.g., A = FUNCTION(formula for diagonal elements, formula for nondiagonal elements)?
I'd appreciate any and all help. Thank you
Best, John

採用された回答

Roger Stafford
Roger Stafford 2015 年 4 月 2 日
A = eye(length(M))*f1+(1-eye(length(M))*f2;
where f1 is the formula for the diagonal elements and f2 that for the off-diagonal elements.
  2 件のコメント
Roger Stafford
Roger Stafford 2015 年 4 月 2 日
The following is preferable. Let M be a row vector of size 1-by-m.
A = diag(M.*(1+(M-1)*roh_w))+(1-eye(m)).*(M.'*(M*roh_b));
Jeong Ho
Jeong Ho 2015 年 4 月 2 日
Dear Roger,
Thank you so much for your help. It works like a charm! Thank you!
Best,
John

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by