2016-11-06 78 views
0

我有一个矢量在某些地方有1,我想用矢量创建一个对角线。该载体被称为one_vec_two为什么'spdiags`不能将矢量放在正确的位置?

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

按照该文档为spdiag

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) 

A = spdiags(non_zero_vecs, -placement, n*n, n*n)' 

这都做同样的事情,只是方式略有不同。

+0

我明白了......谢谢 – Sother

相关问题