Code covered by the BSD License  

Highlights from
REPLICATE

4.0

4.0 | 1 rating Rate this file 6 Downloads (last 30 days) File Size: 1.76 KB File ID: #22319

REPLICATE

by Mark Ruzon

 

04 Dec 2008 (Updated 08 Dec 2008)

Create a larger array by expanding each element of an array into a subarray.

| Watch this File

File Information
Description

REPLICATE is a companion to REPMAT, but instead of tiling a larger array with the input array, REPLICATE does the equivalent of calling REPMAT on each individual element and then tile the results together. It is faster than looping over each element. It supports 2D and 3D arrays.

Acknowledgements
This submission has inspired the following:
EXPAND
MATLAB release MATLAB 7.5 (R2007b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
05 Dec 2008 Jos (10584)

Nice try, but KRON can handle the job perfectly, for instance, kron(A,ones([2, 3]))

05 Dec 2008 Mark Ruzon

KRON works for floats and doubles but not integral types, structs, or cell arrays. KRON relies on multiplication, while REPLICATE uses only indexing, thus making it more flexible.

06 Dec 2008 Jos (10584)

Indexing with KRON is equally flexible:
% data
A = {12 {'yy' 'xxx'} ; 'aaa' rand(2)}
nr = 3 ;
nc = 5 ;
% engine
B = A(kron(reshape(1:numel(A),size(A)),ones([nr nc])))

08 Dec 2008 Mark Ruzon

This is a more elegant and faster solution for the 2D case. The next version of the file will have this solution. Unfortunately, KRON gives 2D results when Y is 3D, so a different approach is needed in the 3D case.
A = reshape(1:6,[1 3 2]);
S = [2 2 2];
A(kron(reshape(1:numel(A),size(A)),ones(S)))
replicate(A,S)

04 Aug 2009 Russell

I used replicate.m to generate some padding on a string cell array.

AA = {'blank'};
myLabels = {'fish','beer'};
myLabels = [ myLabels (replicate (AA,1,3)) ]
myLabels =
    'fish' 'beer' 'blank' 'blank' 'blank'

Please login to add a comment or rating.
Updates
04 Dec 2008

Bug fix: renamed input parameter.

08 Dec 2008

Sped up 2D case using KRON. Thanks go to Jos.

Tag Activity for this File
Tag Applied By Date/Time
matrices Mark Ruzon 04 Dec 2008 16:08:14
repmat Mark Ruzon 04 Dec 2008 16:08:14
replicate Mark Ruzon 04 Dec 2008 16:08:14
tile Mark Ruzon 04 Dec 2008 17:12:08
matrices yang zhengfa 04 Dec 2008 23:57:41
matrices Cristina McIntire 05 Dec 2008 14:35:15
replicate Cristina McIntire 05 Dec 2008 14:35:15

Contact us at files@mathworks.com