Matrix define using for loop
조회 수: 1 (최근 30일)
이전 댓글 표시
How to define the following matrix (penta diagonal) using for loop?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/337954/image.png)
댓글 수: 2
Walter Roberson
2020년 7월 27일
Why use a for loop when Bruno already showed you a vectorized solution?
채택된 답변
KSSV
2020년 7월 27일
Read about diag.
A = -4*ones(9,1) ;
B = [1 1 0 1 1 0 1 1] ;
C = ones(1,6) ;
iwant = diag(C,-3)+ diag(B,-1) + diag(A) +diag(B,1) + diag(C,3)
댓글 수: 2
KSSV
2020년 7월 27일
You can proceed like this:
% define the constants
F0 = rand ;
S = rand ;
dr = 1 ;
%
A = (2+4*F0)*ones(1,9) ;
B = F0*ones(8,1) ; B(3:3:end) = 0 ;
id = [2 2 2 3 3 3 ] ;
C = -F0-S./(dr*id) ;
id = [3 3 3 4 4 4 ] ;
D = -F0+S./(dr*id) ;
iwant = diag(D,-3)+diag(B,-1)+diag(A)+diag(B,1)+diag(C,3) ;
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!