Stata 13 help for mf_swap


[M-5] swap() -- Interchange contents of variables


void swap(transmorphic matrix A, transmorphic matrix B)


swap(A, B) interchanges the contents of A and B. A and B are not required to be of the same type or dimension.


There is no faster way than swap(A, B) to assign A=B when you do not care about the contents of B after the assignment. For instance, you have the code

A = B B = ...(matrix expression)...

Faster is

swap(A, B) B = ...(matrix expression)...

The execution time of swap() is independent of the size of A and B, and swap() conserves memory to boot. Pretend that B is a 900 x 900 matrix. A=B is executed, but before B is reassigned, two copies of the 900 x 900 matrix exist. That does not happen with swap().


swap(A, B): input: A: r1 x c1 B: r2 x c2 output A: r2 x c2 B: r1 x c1


swap(A, B) works only with variables. Do not code, for instance, swap(A[i,j], A[j,i]). It is not an error, but it will have no effect.

Source code

Function is built in.

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index