99x99 matrix
3 ビュー (過去 30 日間)
古いコメントを表示
create a 99x99 matrix with ones on both diagonals and zeros everywhere otherwise
0 件のコメント
回答 (4 件)
Image Analyst
2021 年 10 月 15 日
Another way, even more compact:
A = eye(99) | fliplr(eye(99))
As long as it's not your homework you can use my code.
Image Analyst
2021 年 10 月 15 日
As long as it's not your homework you can use my code:
A = min(1, eye(99) + fliplr(eye(99)))
0 件のコメント
Chunru
2021 年 10 月 15 日
% For time comparison:
n = 1000;
timeit(@() bidiag1(n))
timeit(@() bidiag2(n))
timeit(@() bidiag3(n))
function bidiag1(n)
a = eye(n);
a(n:n-1:n*n-1) = 1;
end
function bidiag2(n)
a = eye(n) | fliplr(eye(n));
end
function bidiag3(n)
a = min(1, eye(n) + fliplr(eye(n)));
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!