how to create nxn matrix with main diagonal that enters odd rows/columns with zero and even ones start with 2(n+1) sequence
3 ビュー (過去 30 日間)
古いコメントを表示
im trying to write a code where even main diagonal entries start off with 2 and continue 4,6,8.... etc but i only get outputs of 2. im assuming my problem is with the n=n+1 counter but im not sure.
my code is = for i=1:30;
For j=1:30;
for i=j
c=1;
if mod(i,2)~0 A(i,j)=2*c c=c+1; if mod (i, 2) ==0
A(i, j)=0;
end
end
end end
end
0 件のコメント
回答 (2 件)
John D'Errico
2024 年 5 月 15 日
Simple is to use diag.
n = 5; % Now many non-zero elements will we have?
N = 1:(2*n); % a simple index vector
D = N.*mod(N-1,2); % create the elements of the main diagonal
A = diag(D)
0 件のコメント
参考
カテゴリ
Help Center および 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!