matrix automation problem

1 回表示 (過去 30 日間)
joey vergara
joey vergara 2012 年 5 月 2 日
[c21 c11 c21 0 0 0 0 0 0 0; 0 c21 c11 c21 0 0 0 0 0 0; 0 0 c21 c11 c21 0 0 0 0 0; 0 0 0 c21 c11 c21 0 0 0 0; 0 0 0 0 c21 c11 c21 0 0 0; 0 0 0 0 0 c21 c11 c21 0 0; 0 0 0 0 0 0 c21 c11 c21 0; 0 0 0 0 0 0 0 c21 c11 c21] where the amount of rows/columns depends on a variable m and n respectively m=n-2
and n=10 in this case
how do i "automate" this??? TIA

回答 (1 件)

Richard Brown
Richard Brown 2012 年 5 月 2 日
This is one annoying case where the sparse version spdiags can do more than the full one diag
n = 10;
m = n-2;
c11 = 1;
c21 = 2;
B = repmat([c21 c11 c21], m, 1);
A = spdiags(B, [0 1 2], m, n);
You might want to use
A = full(spdiags(B, [0 1 2], m, n));
if you don't want a sparse matrix.

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by