99x99 matrix
이전 댓글 표시
create a 99x99 matrix with ones on both diagonals and zeros everywhere otherwise
답변 (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.
댓글 수: 1
Chetan Bhavsar
2021년 10월 15일
great wayorring flip i liked it
n = 9; %99
A = eye(n);
A(n:n-1:n*n-1) = 1; % anti-diagonal
A
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)))
% 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
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!