time | calls | line |
---|
| | 1 | function B = repmat(A,varargin)
|
| | 2 | %REPMAT Replicate and tile an array.
|
| | 3 | % B = repmat(A,M,N) creates a large matrix B consisting of an M-by-N
|
| | 4 | % tiling of copies of A. The size of B is [size(A,1)*M, size(A,2)*N].
|
| | 5 | % The statement repmat(A,N) creates an N-by-N tiling.
|
| | 6 | %
|
| | 7 | % B = REPMAT(A,[M N]) accomplishes the same result as repmat(A,M,N).
|
| | 8 | %
|
| | 9 | % B = REPMAT(A,[M N P ...]) tiles the array A to produce a
|
| | 10 | % multidimensional array B composed of copies of A. The size of B is
|
| | 11 | % [size(A,1)*M, size(A,2)*N, size(A,3)*P, ...].
|
| | 12 | %
|
| | 13 | % REPMAT(A,M,N) when A is a scalar is commonly used to produce an M-by-N
|
| | 14 | % matrix filled with A's value and having A's CLASS. For certain values,
|
| | 15 | % you may achieve the same results using other functions. Namely,
|
| | 16 | % REPMAT(NAN,M,N) is the same as NAN(M,N)
|
| | 17 | % REPMAT(SINGLE(INF),M,N) is the same as INF(M,N,'single')
|
| | 18 | % REPMAT(INT8(0),M,N) is the same as ZEROS(M,N,'int8')
|
| | 19 | % REPMAT(UINT32(1),M,N) is the same as ONES(M,N,'uint32')
|
| | 20 | % REPMAT(EPS,M,N) is the same as EPS(ONES(M,N))
|
| | 21 | %
|
| | 22 | % Example:
|
| | 23 | % repmat(magic(2), 2, 3)
|
| | 24 | % repmat(uint8(5), 2, 3)
|
| | 25 | %
|
| | 26 | % Class support for input A:
|
| | 27 | % float: double, single
|
| | 28 | % integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64
|
| | 29 | % char, logical
|
| | 30 | %
|
| | 31 | % See also BSXFUN, MESHGRID, ONES, ZEROS, NAN, INF.
|
| | 32 |
|
| | 33 | % Copyright 1984-2013 The MathWorks, Inc.
|
| | 34 |
|
| 115 | 35 | if nargin < 2
|
| | 36 | error(message('MATLAB:minrhs'))
|
| 115 | 37 | elseif nargin == 2
|
| | 38 | M = varargin{1};
|
| | 39 | if isscalar(M)
|
| | 40 | siz = [M M];
|
| | 41 | elseif isempty(M)
|
| | 42 | siz = [1 1];
|
| | 43 | warning(message('MATLAB:repmat:emptyReplications'));
|
| | 44 | elseif isrow(M)
|
| | 45 | siz = M;
|
| | 46 | else
|
| | 47 | error(message('MATLAB:repmat:invalidReplications'));
|
| | 48 | end
|
| | 49 | siz = double(siz);
|
| 115 | 50 | else % nargin > 2
|
| 115 | 51 | if ~all(cellfun(@isscalar,varargin))
|
| | 52 | if nargin == 3 % this code maintains backward compatibility
|
| | 53 | M = varargin{1};
|
| | 54 | N = varargin{2};
|
| | 55 | if (isrow(M) && size(M,2)>1 && (isrow(N) || isempty(N))) || ...
|
| | 56 | (isrow(M) && size(M,2)>0 && isrow(N) && size(N,2)>1)
|
| | 57 | siz = [M N];
|
| | 58 | warning(message('MATLAB:repmat:rowReplications'));
|
| | 59 | elseif isempty(N) && numel(M) <= 1
|
| | 60 | siz = [M 1 1];
|
| | 61 | warning(message('MATLAB:repmat:emptyReplications'));
|
| | 62 | else
|
| | 63 | error(message('MATLAB:repmat:invalidReplications'));
|
| | 64 | end
|
| | 65 | else
|
| | 66 | error(message('MATLAB:repmat:invalidReplications'));
|
| | 67 | end
|
| 115 | 68 | else
|
0.01 | 115 | 69 | siz = cellfun(@(x)double(full(x)),varargin);
|
| 115 | 70 | end
|
| 115 | 71 | end
|
| | 72 |
|
| 115 | 73 | if isscalar(A) && ~isobject(A)
|
| 6 | 74 | nelems = prod(siz);
|
| 6 | 75 | if nelems>0 && nelems < (2^31)-1 % use linear indexing for speed.
|
| | 76 | % Since B doesn't exist, the first statement creates a B with
|
| | 77 | % the right size and type. Then use scalar expansion to
|
| | 78 | % fill the array. Finally reshape to the specified size.
|
0.01 | 6 | 79 | B(nelems) = A;
|
| 6 | 80 | if ~isequal(B(1), B(nelems)) || ~(isnumeric(A) || islogical(A))
|
| | 81 | % if B(1) is the same as B(nelems), then the default value filled in for
|
| | 82 | % B(1:end-1) is already A, so we don't need to waste time redoing
|
| | 83 | % this operation. (This optimizes the case that A is a scalar zero of
|
| | 84 | % some class.)
|
| 6 | 85 | B(:) = A;
|
| 6 | 86 | end
|
| 6 | 87 | B = reshape(B,siz);
|
| | 88 | elseif all(siz > 0) % use general indexing, cost of memory allocation dominates.
|
| | 89 | ind = num2cell(siz);
|
| | 90 | B(ind{:}) = A;
|
| | 91 | if ~isequal(B(1), B(ind{:})) || ~(isnumeric(A) || islogical(A))
|
| | 92 | B(:) = A;
|
| | 93 | end
|
| | 94 | else
|
| | 95 | B = A(ones(siz));
|
| | 96 | end
|
| 109 | 97 | elseif ismatrix(A) && numel(siz) == 2
|
| 109 | 98 | [m,n] = size(A);
|
| 109 | 99 | if (m == 1 && siz(2) == 1)
|
| 109 | 100 | B = A(ones(siz(1), 1), :);
|
| | 101 | elseif (n == 1 && siz(1) == 1)
|
| | 102 | B = A(:, ones(siz(2), 1));
|
| | 103 | else
|
| | 104 | mind = (1:m)';
|
| | 105 | nind = (1:n)';
|
| | 106 | mind = mind(:,ones(1,siz(1)));
|
| | 107 | nind = nind(:,ones(1,siz(2)));
|
| | 108 | B = A(mind,nind);
|
| | 109 | end
|
| | 110 | else
|
| | 111 | Asiz = size(A);
|
| | 112 | Asiz = [Asiz ones(1,length(siz)-length(Asiz))];
|
| | 113 | siz = [siz ones(1,length(Asiz)-length(siz))];
|
| | 114 | subs = cell(1,length(Asiz));
|
| | 115 | for i=length(Asiz):-1:1
|
| | 116 | ind = (1:Asiz(i))';
|
| | 117 | subs{i} = ind(:,ones(1,siz(i)));
|
| | 118 | end
|
| | 119 | B = A(subs{:});
|
| | 120 | end
|