フィルターのクリア

99x99 matrix

3 ビュー (過去 30 日間)
Luke chin
Luke chin 2021 年 10 月 15 日
回答済み: Chunru 2021 年 10 月 15 日
create a 99x99 matrix with ones on both diagonals and zeros everywhere otherwise

回答 (4 件)

Image Analyst
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.
  1 件のコメント
C B
C B 2021 年 10 月 15 日
great wayorring flip i liked it

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


Chunru
Chunru 2021 年 10 月 15 日
n = 9; %99
A = eye(n);
A(n:n-1:n*n-1) = 1; % anti-diagonal
A
A = 9×9
1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1

Image Analyst
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)))

Chunru
Chunru 2021 年 10 月 15 日
% For time comparison:
n = 1000;
timeit(@() bidiag1(n))
ans = 4.1086e-04
timeit(@() bidiag2(n))
ans = 0.0027
timeit(@() bidiag3(n))
ans = 0.0019
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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by