Matlab单位移位矩阵 [英] Matlab identity shift matrix

查看:47
本文介绍了Matlab单位移位矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何内联命令可以在 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]

circshifteye 的组合很好,但是它需要另一个命令来修复它.有更简单的方法吗?(只需一种简单的语法)

combination of circshift and eye is good however it needs another command to fix it. Any simpler way? (with just one simple syntax)

推荐答案

尝试使用 diag 调用结合 一个.对于您的情况,您有一个 10 x 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

<小时>

上面的代码首先声明一个全为 1 的列向量,但我们需要其中的 n-abs(shift) ,因为向右移动意味着我们需要更少的 1填满东西(稍后会详细介绍).n-abs(shift) 也对应于矩阵的总行数/列数,并减去向右移动的次数.接下来,您可以使用 diag,其中第一个参数是一个列向量,它创建一个 zero 矩阵并将列向量作为系数沿该矩阵的对角线放置.第二个参数(在您的情况下为 shift)允许您偏移放置此列的位置.指定一个正值意味着将对角线向右移动,在我们的例子中,我们通过 shift 将其向右移动,因此我们的输出结果.由于您实际上是在向右移动的每个位置截断向量,因此您需要将向量中 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.

到目前为止,我还没有解释为什么在最后一行代码中需要 absshift 的调用.需要调用 abs 的原因是为了适应负偏移.如果我们在第三行代码中没有 abs 调用,则 n-shift 本质上是向向量添加更多的 1,并且因此会将我们的矩阵扩展到 nxn 之外.因为将对角线向左移动也会减少结果中看到的 1 的数量,这就是为什么需要调用 abs 但您会注意到留下了 shift 常量diag 的第二个参数中保持不变.

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.

这是一个带有负移位的演示,shift = -1,并且仍然保持大小为 10 x 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆