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-2016 The MathWorks, Inc.
|
| | 34 |
|
< 0.001 | 13 | 35 | if nargin < 2
|
| | 36 | error(message('MATLAB:minrhs'))
|
< 0.001 | 13 | 37 | elseif nargin == 2
|
| | 38 | M = varargin{1};
|
| | 39 | checkSizesType(M);
|
| | 40 | if isscalar(M)
|
| | 41 | siz = [M M];
|
| | 42 | elseif isempty(M)
|
| | 43 | siz = [1 1];
|
| | 44 | warning(message('MATLAB:repmat:emptyReplications'));
|
| | 45 | elseif isrow(M)
|
| | 46 | siz = M;
|
| | 47 | else
|
| | 48 | error(message('MATLAB:repmat:invalidReplications'));
|
| | 49 | end
|
| | 50 | siz = double(full(siz));
|
< 0.001 | 13 | 51 | else % nargin > 2
|
< 0.001 | 13 | 52 | siz = zeros(1,nargin-1);
|
< 0.001 | 13 | 53 | for idx = 1:nargin-1
|
< 0.001 | 26 | 54 | arg = varargin{idx};
|
< 0.001 | 26 | 55 | if isscalar(arg)
|
< 0.001 | 26 | 56 | siz(idx) = double(full(checkSizesType(arg)));
|
| | 57 | else
|
| | 58 | siz = checkMultipleNonscalarSizesType(varargin);
|
| | 59 | break
|
< 0.001 | 26 | 60 | end
|
< 0.001 | 26 | 61 | end
|
< 0.001 | 13 | 62 | end
|
| | 63 |
|
< 0.001 | 13 | 64 | if isscalar(A) && ~isobject(A)
|
< 0.001 | 1 | 65 | nelems = prod(siz);
|
< 0.001 | 1 | 66 | if nelems>0 && nelems < (2^31)-1 % use linear indexing for speed.
|
| | 67 | % Since B doesn't exist, the first statement creates a B with
|
| | 68 | % the right size and type. Then use scalar expansion to
|
| | 69 | % fill the array. Finally reshape to the specified size.
|
< 0.001 | 1 | 70 | B(nelems) = A;
|
< 0.001 | 1 | 71 | if ~isequal(B(1), B(nelems)) || ~(isnumeric(A) || islogical(A))
|
| | 72 | % if B(1) is the same as B(nelems), then the default value filled in for
|
| | 73 | % B(1:end-1) is already A, so we don't need to waste time redoing
|
| | 74 | % this operation. (This optimizes the case that A is a scalar zero of
|
| | 75 | % some class.)
|
< 0.001 | 1 | 76 | B(:) = A;
|
< 0.001 | 1 | 77 | end
|
< 0.001 | 1 | 78 | B = reshape(B,siz);
|
| | 79 | elseif all(siz > 0) % use general indexing, cost of memory allocation dominates.
|
| | 80 | ind = num2cell(siz);
|
| | 81 | B(ind{:}) = A;
|
| | 82 | if ~isequal(B(1), B(ind{:})) || ~(isnumeric(A) || islogical(A))
|
| | 83 | B(:) = A;
|
| | 84 | end
|
| | 85 | else
|
| | 86 | B = A(ones(siz));
|
| | 87 | end
|
< 0.001 | 12 | 88 | elseif ismatrix(A) && numel(siz) == 2
|
< 0.001 | 12 | 89 | [m,n] = size(A);
|
< 0.001 | 12 | 90 | if (m == 1 && siz(2) == 1)
|
< 0.001 | 12 | 91 | B = A(ones(siz(1), 1), :);
|
| | 92 | elseif (n == 1 && siz(1) == 1)
|
| | 93 | B = A(:, ones(siz(2), 1));
|
| | 94 | else
|
| | 95 | mind = (1:m)';
|
| | 96 | nind = (1:n)';
|
| | 97 | mind = mind(:,ones(1,siz(1)));
|
| | 98 | nind = nind(:,ones(1,siz(2)));
|
| | 99 | B = A(mind,nind);
|
| | 100 | end
|
| | 101 | else
|
| | 102 | Asiz = size(A);
|
| | 103 | Asiz = [Asiz ones(1,length(siz)-length(Asiz))];
|
| | 104 | siz = [siz ones(1,length(Asiz)-length(siz))];
|
| | 105 | subs = cell(1,length(Asiz));
|
| | 106 | for i=length(Asiz):-1:1
|
| | 107 | ind = (1:Asiz(i))';
|
| | 108 | subs{i} = ind(:,ones(1,siz(i)));
|
| | 109 | end
|
| | 110 | B = A(subs{:});
|
< 0.001 | 12 | 111 | end
|
Other subfunctions in this file are not included in this listing.