Change an element in diagonal of matrix
古いコメントを表示
As example
A=magic(5);
result=diag(A,-1);
How can I change an element of this diagonal array when I don't know their indices?
1 件のコメント
Kirby Fears
2015 年 11 月 25 日
The size of diag(A,n) will always be (length(A)-abs(n)) by 1.
The indices of your diagonal array are 1 through 4. They correspond to elements (2,1), (3,2), (4,3), and (5,4) of A. This correspondence can be formulated just in terms of n and the size of A.
Does this help? I'm not sure what information you are seeking.
採用された回答
その他の回答 (2 件)
Andrei Bobrov
2015 年 11 月 25 日
e.g.:
A=magic(5);
ii = diag(true(4,1),-1);
A(ii) = 100;
Sean de Wolski
2015 年 11 月 25 日
編集済み: Sean de Wolski
2015 年 11 月 25 日
The -1st diagonal is 2:n+1:end. You can index into that directly without ever needing diag/tril
n = 5;
x = magic(n);
didx = 2:n+1:numel(x);
% Change third element on -1st diagonal
x(didx(3)) = -100
x =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 -100 21 3
11 18 25 2 9
カテゴリ
ヘルプ センター および File Exchange で Operating on Diagonal Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!