Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab identity shift matrix

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]

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

like image 533
zahmati Avatar asked May 14 '15 06:05

zahmati


1 Answers

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

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.

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.

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
like image 72
rayryeng Avatar answered Oct 03 '22 07:10

rayryeng