Matlab的身份转变矩阵 [英] Matlab identity shift matrix
问题描述
是否有任何内嵌命令生成在MATLAB转移矩阵?
Is there any inline command to generate shifted identity matrix in MATLAB?
A=[ ...
0, 1, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 1, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 1, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 1, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 1, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 1, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 1, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 1, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
circshift
的组合和眼
是好的但它需要另一个命令来修复它。任何简单的方法? (只用一个简单的语法)
combination of circshift
and eye
is good however it needs another command to fix it. Any simpler way? (with just one simple syntax)
推荐答案
尝试使用的 诊断
结合的 那些
。对于你的情况,你有一个10×10矩阵,并希望通过1对角线向右移动。
Try using a diag
call in combination with ones
. For your case, you have a 10 x 10 identity matrix and want to shift the diagonal to the right by 1.
>> n = 10;
>> shift = 1;
>> A = diag(ones(n-abs(shift),1),shift)
A =
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
以上code ++工程,首先宣布全1的列向量,但是我们需要正ABS(移)
他们作为向右移动意味着我们将需要较少的1秒(后面有更多这一点),以填补事情了。 正ABS(移)
也符合你的矩阵,并减去尽可能多的时候,你正在转变朝着正确的行/列的总数。接下来,你可以使用诊断
,其中第一个参数是它创建了一个零矩阵和列向量放在沿对角线的系数列向量这个矩阵。第二个参数(移
你的情况),可以为偏移在何处放置此列。指定正值意味着向右移动对角线,并在我们的例子中,我们通过移
此移动到右侧,因此我们的输出结果。当你基本上是为截断向你移动的右各个位置向量,则需要通过这么多,以减少在您的载体中1的个数。
The above code works by first declaring a column vector of all 1s, but we would need n-abs(shift)
of them as moving to the right would mean that we would require less 1s to fill things up (more on this later). n-abs(shift)
also corresponds to the total number of rows/columns of your matrix and subtracting out as many times you are shifting towards the right. Next, you can use diag
where the first parameter is a column vector which creates a zero matrix and places the column vector as coefficients along the diagonal of this matrix. The second parameter (shift
in your case) allows you to offset where to place this column. Specifying a positive value means to move the diagonals towards the right, and in our case we are moving this to the right by shift
, and hence our output results. As you are essentially truncating the vector for each position towards the right you are moving, you would need to decrease the number of 1s in your vector by this much.
到现在为止,我还没有解释为什么在 ABS
来电移
的最后一行是必需的的code。为什么需要 ABS
调用的原因是,以适应负的变化。如果我们没有足够的 ABS
呼叫code的第三行,正移
基本上会是的添加 1秒多的载体,从而将扩大我们的矩阵超越 n×n个
。由于移动对角线到左侧也降低了结果看到1秒的量,这就是为什么需要 ABS
呼叫,但你会发现,在移
恒在诊断
的第二个参数保持不变。
Up to now, I haven't explained why the abs
call to shift
is required in the last line of code. The reason why the abs
call is required is to accommodate for negative shifts. If we didn't have the abs
call in the third line of code, n-shift
would essentially be adding more 1s to the vector and would thus expand our matrix beyond n x n
. Because moving the diagonals to the left also decreases the amount of 1s seen in the result, that's why the abs
call is required but you'll notice that the shift
constant is left untouched in the second parameter of diag
.
下面是带有负转变演示,移= -1
,并仍保持尺寸为10×10:
Here's a demonstration with a negative shift, shift = -1
, and still maintaining the size to be 10 x 10:
A =
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
这篇关于Matlab的身份转变矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!