help mata mm_expand()-------------------------------------------------------------------------------

Title

mm_expand() -- Duplicate rows and columns

Syntax

t. matrixmm_expand(X,real vector nr[,real vector nc,sort])

void_mm_expand(X,real vector nr,real vector nc,sort)

t. matrixmm_repeat(X,real scalar nr[,real scalar nc])

void_mm_repeat(X,real scalar nr,real scalar nc)

where

Xis a transmorphic matrix andsortis a real scalar.

Description

mm_expand()duplicates rows and columns inXand returns the result as a new matrix. Vectornrspecifies for each row the desired number of duplicates (with 1 indicating that no copies are added).nrmay also be scalar, in which casenrduplicates are returned of each row.ncanalogously specifies the column duplicates.The default for

mm_expand()is to append additional copies of rows at the bottom ofXand append additional copies of columns at the right. Alternatively,sort!=0 causes duplicates to be tied together and arranged in the original sort order ofX.

mm_repeat()returns a matrix composed of repetitions ofX.nrspecifies the desired number of repetitions ofXin the direction of rows.ncspecifies the desired number of repetitions ofXin the direction of columns.

_mm_expand()and_mm_repeat()expandXin place.With all of the above functions, values less than 1 or missing values are interpreted as if they were 1 in

nrandnc. See help forselect()if you want to delete rows or columns from a matrix.With the 25feb2008 update of Stata 10 a matrix version of the

J()function has been introduced, which supersedes themm_repeat()function.

RemarksSome examples:

: X = (1,2)' : X 1 +-----+ 1 | 1 | 2 | 2 | +-----+

: mm_expand(X,(3,2)) 1 +-----+ 1 | 1 | 2 | 2 | 3 | 1 | 4 | 1 | 5 | 2 | +-----+

: mm_expand(X,(3,2),1,1) 1 +-----+ 1 | 1 | 2 | 1 | 3 | 1 | 4 | 2 | 5 | 2 | +-----+

: mm_expand(X',2,(3,2)) 1 2 3 4 5 +---------------------+ 1 | 1 2 1 1 2 | 2 | 1 2 1 1 2 | +---------------------+

: mm_expand(X',2,(3,2),1) 1 2 3 4 5 +---------------------+ 1 | 1 1 1 2 2 | 2 | 1 1 1 2 2 | +---------------------+

: S = ("a","b")\("c","d") : S 1 2 +---------+ 1 | a b | 2 | c d | +---------+

: mm_repeat(S,3,2) 1 2 3 4 +-----------------+ 1 | a b a b | 2 | c d c d | 3 | a b a b | 4 | c d c d | 5 | a b a b | 6 | c d c d | +-----------------+

: _mm_repeat(S,3,2) : S 1 2 3 4 +-----------------+ 1 | a b a b | 2 | c d c d | 3 | a b a b | 4 | c d c d | 5 | a b a b | 6 | c d c d | +-----------------+

DiagnosticsNon-integer values in

nrandncare truncated.

Source codemm_expand.mata

AuthorBen Jann, ETH Zurich, jann@soz.gess.ethz.ch

Also seeOnline: help for

[M-5] select(),moremata