2016-11-06 4 views
0

私はある場所に1のベクトルを持っており、そのベクトルを使って対角を作りたいと思っています。ベクターone_vec_twoの最初の要素は1である`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呼ばれる

>> one_vec_two(1) 

ans = 

    1 

そして、Iは3ある対角線n-1から始まるベクトルを、置きました。しかし、私は列4に行くとき、私はそれを見ることはありません。

>> fullA(1,4) 

ans = 

    0 

はなぜMATLABが正しい場所に私のベクトルを入れていませんか?

答えて

1

それが指定された場所にあなたのベクトルの下部部分を確定さ

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.

spdiag用DOC 1として。したがって、結果は期待通りです。

あなたは

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

関連する問題