简体   繁体   English

100x100矩阵MATLAB编码

[英]100x100 Matrix MATLAB coding

I would like to generate a 100x100 Matrix that is similar to this whereby the first line and last line are different and the middle ones are all the same but shifted along by 1. 我想生成一个与此类似的100x100矩阵,其中第一行和最后一行是不同的,而中间的行都相同,但移动了1。

Here is what I have for a 10x10 matrix: 这是我对10x10矩阵的要求:

>> A=[0.8 -0.2 0 0 0 0 0 0 0 0;
-0.3 0.5 -0.2 0 0 0 0 0 0 0;
0 -0.3 0.5 -0.2 0 0 0 0 0 0;
0 0 -0.3 0.5 -0.2 0 0 0 0 0;
0 0 0 -0.3 0.5 -0.2 0 0 0 0;
0 0 0 0 -0.3 0.5 -0.2 0 0 0;
0 0 0 0 0 -0.3 0.5 -0.2 0 0;
 0 0 0 0 0 -0.3 0.5 -0.2 0;
0 0 0 0 0 0 0 -0.3 0.5 -0.2;
0 0 0 0 0 0 0 0 -0.3 0.7;]

B= [62; 0; 0; 0; 0; 0; 0; 0; 0; 82]

>> solution=inv(A)*B

Any help is much appreciated. 任何帮助深表感谢。

Many thanks. 非常感谢。

What you want is called a Band Matrix also see this 您想要的被称为带矩阵也可以看到这个

n=10;
e=ones(n,1);
A=spdiags([-0.3*e 0.5*e -0.2*e],-1:1,n,n)

now this is a sparse Matrix, where the zeros are not stored which can improve storage and speed. 现在这是一个稀疏矩阵,其中不存储零,可以提高存储速度。 If you want a full matrix, simply use A=full(spdiags(...)) . 如果您需要一个完整的矩阵,只需使用A=full(spdiags(...))

For B do: 对于B请执行以下操作:

B=ones(10,1)*0.8;
B(1) =62;
B(10)=82;

code to create A is: 创建A的代码是:

A = zeros(100);
A(1,1:2) = [0.8 -0.2];
for i = 2:99
    A(i,i-1:i+1) = [-0.3 0.5 -0.2];
end
A(100,99:100) = [-0.3 0.7];

you can then do B with the same template. 然后,您可以使用相同的模板进行B操作。

Approach 1: 方法1:

%%// Only this part would change when you go from your sample size of 10 to 100
N = 100; 

A = zeros(N); %%// Initialize
A(1:size(A,1)+1:end) = 0.5; %%// Diagonal values
A(2:size(A,1)+1:end) = -0.3;%%// Left-to-diagonal values
A(size(A,1)+1:size(A,1)+1:end) = -0.2;%%// Right-to-diagonal values
A([1 end]) = [0.8 0.7]; %% Different scalars at the top and end

Approach 2: 方法二:

N = 100; %%// Size of matrix

L = -0.3; %%// Left to diagonal values
D = 0.5;  %%// Diagonal values
R = -0.2;  %%// Right to diagonal values

A = D*diag(ones(N,1)) + R*diag(ones(N-1,1),1) + L*diag(ones(N-1,1),-1);
A([1 end]) = [0.8 0.7]; %% Different scalars at the top and end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM