필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by