在matlab中创建特定的大矩阵

时间:2013-04-02 23:39:46

标签: matlab

我的问题是如何在matlab程序中创建相同类型的矩阵,使其保持相同的逻辑,即主对角线上的10s,然后围绕主对角线的上下对角线中的3s,以及在其他情况下3s和0s之上和之下的对角线上,我可以修改我喜欢的NxN吗?

对于6x6案例

这样的事情

A = [10  3  1  0  0  0 ; 
      3 10  3  1  0  0 ;
      1  3 10  3  1  0 ; 
      0  1  3 10  3  1 ; 
      0  0  1  3 10  3 ;
      0  0  0  1  3 10 ];

2 个答案:

答案 0 :(得分:3)

代码:

toeplitz([10  3  1  0  0  0])

输出:

ans =

    10     3     1     0     0     0
     3    10     3     1     0     0
     1     3    10     3     1     0
     0     1     3    10     3     1
     0     0     1     3    10     3
     0     0     0     1     3    10

答案 1 :(得分:3)

对于非常大的矩阵(N = 10000),您必须使用稀疏矩阵 请使用spdiags

考虑以下构造
function A = largeSparseMatrix( N )
%
% construct NxN sparse matrix with 10 on main diagonal, 
% 3 on the first off-diagonals and 1 on the second off-diagonals
%
B = ones(N, 5); % pre-allocate for diagonals
B(:,1) = 10;  % main diagonal d(1) = 0
B(:,2:3) = 3; % first off diagonal
B(:,4:5) = 1; % second off-diagonal
d = [ 0 , 1, -1, 2, -2 ]; % mapping columns of B to diagonals of A
A = spdiags( B, d, N, N ); % TA-DA!

请注意,在B的构造中会忽略A中的某些条目 有关详细信息,请参阅spdiags的{​​{3}}。

相关问题