为什么`spdiags`没有将矢量放在正确的位置?

时间:2016-11-06 01:51:13

标签: matlab vector diagonal

我有一个在某些地方有1的矢量,我想用矢量创建一个对角线。该向量称为one_vec_two

n = 4;

one_vec_two = zeros(n*n, 1);
one_vec_two(1,1) = 1;
for k=0:(n-1)
    one_vec_two(k*n+1, 1) = 1;
end

non_zero_vecs = [one_vec_two];
placement = [n-1];

A = spdiags(non_zero_vecs, placement, n*n, n*n);
fullA = full(A);
disp(A)

向量one_vec_two的第一个元素是1:

>> one_vec_two(1)

ans =

     1

并且,我将矢量从对角n-1开始,即3。但是,当我进入第4栏时,我看不到它:

>> fullA(1,4)

ans =

     0

为什么MATLAB不能将我的矢量放在正确的位置?

1 个答案:

答案 0 :(得分:1)

根据for(i = start + 1; i < stop; i++) {的文档,

spdiag

它将矢量的 lower 部分放入指定的位置。因此结果如预期。

看起来你想要像

这样的东西
Note   In this syntax, if a column of B is longer than the diagonal it is replacing, and m >= n, spdiags takes elements of super-diagonals from the lower part of the column of B, and elements of sub-diagonals from the upper part of the column of B.

A = spdiags(non_zero_vecs([end-placement+1:end 1:end-placement]), placement, n*n, n*n)

两者都做同样的事情,只是略有不同。