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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