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 =
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 10
A也可以置放文字串,如:
>>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