matlab矩阵复制函数
repmat 即 Replicate Matrix ,复制和平铺矩阵,是 MATLAB 里面的一个函数。
B = repmat(A,m,n) %将矩阵A复制m×n块,即B由m×n块A平铺而成
B = repmat(A,[m n]) %与上面一致
B = repmat(A,[m n p...]) %B由m×n×p×…个A块平铺而成
repmat(A,m,n) %当A是一个数a时,该命令产生一个全由a组成的m×n矩阵。
处理大矩阵且内容有重复时使用,其功能是以A的内容堆叠在(MxN)的矩阵B中,B矩阵的大小由MxN及A矩阵的内容决定
如果A是一个3x4x5的矩阵,有B = repmat(A,2,3)则最后的矩阵是6x12x5。
一、repmat(NaN,m,n)等价于NaN(m,n). 二、repmat(single(inf),m,n)等价于inf(m,n,'single'). 三、repmat(int8(0),m,n)等价于zeros(m,n,'int8'). 四、repmat(uint32(1),m,n)等价于ones(m,n,'uint32'). 五、repmat(eps,m,n)等价于eps(ones(m,n)).例如:
一、B = repmat(A,m,n) 将矩阵 A 复制 m×n 块,即把 A 作为 B 的元素,B 由 m×n 个 A 平铺而成。B 的维数是 [size(A,1)*m, size(A,2)*n] 。 >> A = [1,2;3,4] A = 1 2 3 4 >> B = repmat(A,2,3) B = 1 2 1 2 1 2 3 4 3 4 3 4 1 2 1 2 1 2 3 4 3 4 3 4 二、B = repmat(A,[m n]) 与 B = repmat(A,m,n) 用法一致。 三、B = repmat(A,[m n p...]) B 是由 m×n×p×… 个 A 平铺而成的高维数组。B 的维数是 [size(A,1)*m, size(A,2)*n, size(A,3)*p, ...] 。 >> A = eye(2,2) A = 1 0 0 1 >> B = repmat(A,[2 3 2]) B(:,:,1) = 1 0 1 0 1 0 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 B(:,:,2) = 1 0 1 0 1 0 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1>>temp=1:10;
>>a=repmat(temp,10,1)
a = 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
>> b=repmat(temp',1,10)
b = 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10A也可以置放文字串,如:
>>C=repmat(' Long live the king!', 2,2)
C =
Long live the king! Long live the king!
Long live the king! Long live the king!
也可置放其他的:
>> D=repmat(NaN,2,5)
D =
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN