Matrix define using for loop

3 ビュー (過去 30 日間)
mathru
mathru 2020 年 7 月 27 日
コメント済み: KSSV 2020 年 7 月 27 日
How to define the following matrix (penta diagonal) using for loop?
  2 件のコメント
Walter Roberson
Walter Roberson 2020 年 7 月 27 日
Why use a for loop when Bruno already showed you a vectorized solution?
mathru
mathru 2020 年 7 月 27 日
In this matrix, the diagonal value is -4. But for a generalized form of this matrix, I have to use an expression "1/(i*dr)" where dr is the increment along x direction. That's why I need to use for loop.

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

採用された回答

KSSV
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 件のコメント
mathru
mathru 2020 年 7 月 27 日
Hi KSSV, thanks for your code. Here is my generalized matrix. Can you please have a look?
KSSV
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 ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by