how can i write this matrix in matlab?
조회 수: 1 (최근 30일)
이전 댓글 표시
how can i write this matrix in matlab? this matrix is (n+1)*(n+1) that n must be enter by user
댓글 수: 0
채택된 답변
Rick Rosson
2014년 11월 2일
x = zeros(n+1);
x(1,1:2) = [ 2 1 ];
x(end,end-1:end) = [ 1 2 ];
for k = 2:n
x(k,k–1:k+1) = [ 1 4 1 ];
end
댓글 수: 0
추가 답변 (2개)
dpb
2014년 11월 2일
Given input n and
r = [1 4 1];
c={repmat(r,n,1)}; % cell array of n copies of r
m=blkdiag([2 1],c{:},[1 2]); % expand c to comma list for blkdiag
clear c
댓글 수: 0
Image Analyst
2014년 11월 2일
Lots of ways, for loop, addition, assignment, zeros(), etc. I'd probably use eye() and circshift(). Hint:
diagArray = 4 * eye(7)
See what that gives you. Imagine shifting some diagonal arrays with circhsift() and adding the 3 arrays together. That should be enough hint to get you started.
댓글 수: 1
dpb
2014년 11월 3일
I thought the blkdiag solution kinda' cute, IA... :)
Excepting for needing the temporary cell array; couldn't see a simple way to generate the comma-separated list in one swell foop...
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!