简体   繁体   English

Matlab循环矩阵编码

[英]Matlab coding for circulant matrix

I have this Matlab function from van Loan's book Introduction to Scientific Computation. 我从van Loan的《 科学计算简介 》一书中获得了Matlab函数 It produces a matrix C where each row is the previous row with every element shifted by one to the right. 它产生矩阵C,其中每一行是前一行,每个元素向右移动一个。 I want to modify it so that the shift is to the right but I am having some trouble with the logic. 我想对其进行修改,以便向右移动,但是我在逻辑上遇到了一些麻烦。

In particular, does the loop below make sense for right shift? 特别是,下面的循环对右移有意义吗?

for i=2:n
C(i,:)=[C(i-1, n-2) C(i-1, 1:n-1)];
end


function C= circulantShift(a) %shifts to left
a=[1 2 3 4];
n=length(a);
C=zeros(n,n);
C(1,:)=a;
for i=2:n
   C(i, :)=[C(i-1, n) C(i-1, 1:n-1)];
end

In order to shift to the left you need to use: 为了向左移动,您需要使用:

C(i,:)=[C(i-1, 2:n) C(i-1, 1)];


C =

   1   2   3   4
   2   3   4   1
   3   4   1   2
   4   1   2   3

First: the function circulantShift(a) shifts to the right, not the left. 首先: circulantShift(a)函数向右移动,而不是向左移动。

The logic is as follows: In matlab [ab] does a horizontal concatenation. 逻辑如下:在matlab [ab]中进行水平串联。 So [C(i-1, n) C(i-1, 1:n-1)] builds a line that consists of the nth (last) number in the row above, followed by the first 1:n-1 numbers in the row above. 因此, [C(i-1, n) C(i-1, 1:n-1)]建立了一条线,该线由上一行中的第n个(最后一个)数字组成,后接前一个1:n-1个数字在上面的行中。 This is plainly a circular shift to the right. 这显然是向右的循环移位。

If you want to shift to the left, you do the reverse. 如果要向左移动,请执行相反的操作。 You take the 2nd to nth numbers from the row above, followed by the first number from the row above. 您从上一行中选择第2至第n个数字,然后是上一行中的第一个数字。 Like this: 像这样:

[C(i-1, 2:n) C(i-1, 1)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM