﻿ 在MATLAB中移动较大矩阵内的小矩阵 - 代码日志

#### 在MATLAB中移动较大矩阵内的小矩阵

``````>> A = zeros(5)

A =

0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
``````

B是一个小的矩阵(2×2)：

``````>> B = ones(2)

B =

1     1
1     1
``````

``````C1 =

1     1     0     0     0
1     1     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0

C2 =

0     0     0     0     0
1     1     0     0     0
1     1     0     0     0
0     0     0     0     0
0     0     0     0     0

C3 =

0     0     0     0     0
0     0     0     0     0
1     1     0     0     0
1     1     0     0     0
0     0     0     0     0
``````

……最后C16等于：

``````C16 =

0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     1     1
0     0     0     1     1
``````

``````A = [1 1 0 0; 1 1 0 0; 0 0 0 0; 0 0 0 0]
A =

1     1     0     0
1     1     0     0
0     0     0     0
0     0     0     0
Y = circshift(A,[1 1])
Y =

0     0     0     0
0     1     1     0
0     1     1     0
0     0     0     0
``````

``````A=zeros(5,5);
A(1:2,1:2)=1
c=1;C=zeros(5,5,16);
for i=0:3
for j=0:3
C(:,:,c)=circshift(A,[i j])
c=c+1;
end
end
``````

``````A =

1     1     0     0     0
1     1     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0

ans =

1     1     0     0     0
1     1     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0

ans =

0     1     1     0     0
0     1     1     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0

ans =

0     0     1     1     0
0     0     1     1     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0

ans =

0     0     0     1     1
0     0     0     1     1
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0

ans =

0     0     0     0     0
1     1     0     0     0
1     1     0     0     0
0     0     0     0     0
0     0     0     0     0

ans =

0     0     0     0     0
0     1     1     0     0
0     1     1     0     0
0     0     0     0     0
0     0     0     0     0

ans =

0     0     0     0     0
0     0     1     1     0
0     0     1     1     0
0     0     0     0     0
0     0     0     0     0

ans =

0     0     0     0     0
0     0     0     1     1
0     0     0     1     1
0     0     0     0     0
0     0     0     0     0

ans =

0     0     0     0     0
0     0     0     0     0
1     1     0     0     0
1     1     0     0     0
0     0     0     0     0

ans =

0     0     0     0     0
0     0     0     0     0
0     1     1     0     0
0     1     1     0     0
0     0     0     0     0

ans =

0     0     0     0     0
0     0     0     0     0
0     0     1     1     0
0     0     1     1     0
0     0     0     0     0

ans =

0     0     0     0     0
0     0     0     0     0
0     0     0     1     1
0     0     0     1     1
0     0     0     0     0

ans =

0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
1     1     0     0     0
1     1     0     0     0

ans =

0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
0     1     1     0     0
0     1     1     0     0

ans =

0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     1     1     0
0     0     1     1     0

ans =

0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     1     1
0     0     0     1     1
``````