Diagonals in Matrices Matlab

1 回表示 (過去 30 日間)
Buster
Buster 2021 年 9 月 13 日
コメント済み: Buster 2021 年 9 月 13 日
i have a matrix A=zeros(400,400).
m=size(A,1)
A(1:m+1:end) = 1 % main diagonal
A(21:m+1:end) = -.25 % lower diagonal
A(8001:m+1:end) = -.25 %upper diag
A(2:m+1:end) = -.25 %close lower diag
A(401:m+1:end) = -.25 %close upper diag
The first three diagonals are continous and dont skip. However, i need the close lower and upper diagonals to be -.25 for every 2 instances. As in, the 3rd column of both close diagonals will be 0 and then the next two are -.25 followed by another 0 for the entire 400x400 matrix. NOTE: the close upper diagonal starts in column 2 while the lower starts in the first diagonal. EX. i need A(3,4)=0 ...A(6,7)=0... etc i am aware of the pattern i just dont know how solve in matlab

採用された回答

Jan
Jan 2021 年 9 月 13 日
編集済み: Jan 2021 年 9 月 13 日
I'm not sure if I can follow your explanations. A short example might be useful.
The term "be -.25 for every 2 instances" might mean:
A(2:(2*m+2):end) = -0.25; % close lower diag
A(401:(2*m+2):end) = -0.25; % close upper diag
What does this mean: "As in, the 3rd column of both close diagonals will be 0 and then the next two are -.25 followed by another 0"? With some guessing:
A(2:(3*m+3):end) = -0.25;
A(2+m+1:(3*m+3):end) = -0.25;
  1 件のコメント
Buster
Buster 2021 年 9 月 13 日
This actually worked but it did every other instance when i need every three. would i just change the +2 to +3?

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

その他の回答 (0 件)

カテゴリ

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