フィルターのクリア

Code to generate matrix

2 ビュー (過去 30 日間)
Aswas
Aswas 2016 年 5 月 12 日
コメント済み: Aswas 2016 年 5 月 12 日
bold Hello,
I have this matrix, can someone please adv how to write the code:
A = [ 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
-1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 -1 5 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0;
-1 0 0 0 0 0 0 0 0 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0;
0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0;
0 0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0;
0 0 0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0;
0 0 0 0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0;
0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0;
0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0;
0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 ;
0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 5 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 3 -1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 -1 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 -1 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 -1 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 -1 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 -1 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 -1 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 ];
b=[0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;5;0;0;0;0;0;0;0;5];
  3 件のコメント
Aswas
Aswas 2016 年 5 月 12 日
編集済み: Walter Roberson 2016 年 5 月 12 日
Sorry, but I'd like to get this matrix by running a code, so I can expand it to a much bigger matrix, for eg following generates 26 x 26 with zeros:
clc;
clear all;
L=1;
nx = 26;
p=linspace(0,L,nx);
dx=p(nx)-p(nx-1);
%==========Matrix Generation=========================
%Matrix with zeros
A=zeros(nx,nx)
rhs=zeros(nx,1)
Guillaume
Guillaume 2016 年 5 月 12 日
編集済み: Guillaume 2016 年 5 月 12 日
Well, then you have to explain the logic behind the matrix generation.
As far as I can tell b is just a zero column with 5 in two (random?) locations and A is a matrix that could be generated with toeplitz except that some (random?) -1 get replaced by 0 and the main diagonal is either 3, 4 or 5 (depending on ?)

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

採用された回答

Walter Roberson
Walter Roberson 2016 年 5 月 12 日
You can put it together using diag()
d0 = [repmat(4,1,8), 5, repmat(4,1,8), 5, repmat(3,1,8)];
d1 = ones(1, length(d0)-1);
d9 = ones(1, length(d0)-9);
A = diag(d0,0) + diag(d1, -1) + diag(d1, 1) + diag(d9, -9) + diag(d9, 9);
b = [zeros(1,17), 5, zeros(1, 7), 5];
  5 件のコメント
Walter Roberson
Walter Roberson 2016 年 5 月 12 日
d0 = [repmat(4,1,8), 5, repmat(4,1,8), 5, repmat(3,1,8)];
d9 = -ones(1, length(d0)-9);
d1 = [-ones(1,8), 0, -ones(1,8), 0, -ones(1,7)];
A = diag(d0,0) + diag(d1, -1) + diag(d1, 1) + diag(d9, -9) + diag(d9, 9);
For your purpose it might make sense to construct
d1 = one(1,length(d0)-1);
d1(9:9:end) = 0;
Aswas
Aswas 2016 年 5 月 12 日
Thanks Walter, much appreciated.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by