Matlab之repmat和reshape函数

来源:互联网 发布:sql添加默认值约束 编辑:程序博客网 时间:2024/06/12 05:51

repmat:Replicate and tile array 

              B = repmat(A,n)

Description:

1.B = repmat(A,n,m) returns an n-by-m tiling of A. 

  example:Create a diagonal matrix

A=diag([10 20 30])
A =
    10     0     0
     0    20     0
     0     0    30
K>> B=repmat(A,2,3)
B =
    10     0     0    10     0     0    10     0     0
     0    20     0     0    20     0     0    20     0
     0     0    30     0     0    30     0     0    30
    10     0     0    10     0     0    10     0     0
     0    20     0     0    20     0     0    20     0
     0     0    30     0     0    30     0     0    30

结果变成6x9

2.B = repmat(A,sz1,sz2,...,szN) specifies a list of scalars, sz1,sz2,...,szN, to describe an N-D tiling of A. The size of B is [size(A,1)*sz1, size(A,2)*sz2,...,size(A,n)*szN]. For example, repmat([1 2; 3 4],2,3) returns a 4-by-6 matrix.

K>> A=1:5
A =
     1     2     3     4     5
K>> repmat(A,2,4)
ans =
     1     2     3     4     5     1     2     3     4     5     1     2     3     4     5     1     2     3     4     5
     1     2     3     4     5     1     2     3     4     5     1     2     3     4     5     1     2     3     4     5

结果变成:2x20

3.A也可以是字符串

K>> A='you are mine';
K>> repmat(A,2)
ans =
you are mineyou are mine
you are mineyou are mine
简而言之,就是把A矩阵作为一个元素,从而扩大成另一个新的矩阵B。

reshape:Reshape array

Description:

1.B = reshape(A,m,n) or B = reshape(A,[m n]) returns the m-by-n matrix B whose elements are taken column-wise from A. An error results if A does not have m*n elements

Example:

Reshape a 3-by-4 matrix into a 2-by-6 matrix.
A =
    1    4    7    10
    2    5    8    11
    3    6    9    12
          
B = reshape(A,2,6)
          
B =
    1    3    5    7    9   11
    2    4    6    8   10   12
B = reshape(A,2,[ ])
          
B =
    1    3    5    7    9   11
    2    4    6    8   10   12

简而言之,就是在矩阵A元素不变的前提下,把A塑造或者变形成一个矩阵B.

0 0