```help mata mz_reshape()
-------------------------------------------------------------------------------

Title

mz_reshape() -- Remodels a matrix according both to specified rows and
columns

Syntax

transmorphic matrix mz_reshape(x, m, n)

where

x:  transmorphic matrix

m:  real scalar containing the new row dimension

n:  real scalar containing the new column dimension

Description

mz_reshape() returns a new matrix which is the matrix x remodeled to a
matrix with m rows and n columns. mz_reshape() is similar to mata
official built in functions [M-5] colshape() and [M-5] rowshape(). But it
is more flexible in that the user can set the number of rows and columns
he/she wants for the new matrix. That makes that mz_reshape() performs
transformations where both [M-5] colshape() and [M-5] rowshape() returns
errors.  mz_reshape() forms matrices by row. mz_reshape() returns to the
first components of x and gets components from there if there are less
components in x to make the new matrix.  The lingering components are
left over if there are more components in x than in the new matrix.

Remarks

Examples:

: x
1    2    3    4
+---------------------+
1 |  50   51   52   53  |
2 |  54   55   56   57  |
3 |  58   59   60   61  |
4 |  62   63   64   65  |
+---------------------+

: mz_reshape(x, 2, 8)
1    2    3    4    5    6    7    8
+-----------------------------------------+
1 |  50   51   52   53   54   55   56   57  |
2 |  58   59   60   61   62   63   64   65  |
+-----------------------------------------+

: mz_reshape(x, 2, 6)
1    2    3    4    5    6
+-------------------------------+
1 |  50   51   52   53   54   55  |
2 |  56   57   58   59   60   61  |
+-------------------------------+

: mz_reshape(x, 8, 2)
1    2
+-----------+
1 |  50   51  |
2 |  52   53  |
3 |  54   55  |
4 |  56   57  |
5 |  58   59  |
6 |  60   61  |
7 |  62   63  |
8 |  64   65  |
+-----------+

: mz_reshape(x, 6, 2)
1    2
+-----------+
1 |  50   51  |
2 |  52   53  |
3 |  54   55  |
4 |  56   57  |
5 |  58   59  |
6 |  60   61  |
+-----------+

If the matrix x has only one element

: x
1
+------+
1 |  50  |
+------+

We have

: mz_reshape(x, 3, 5)
1    2    3    4    5
+--------------------------+
1 |  50   50   50   50   50  |
2 |  50   50   50   50   50  |
3 |  50   50   50   50   50  |
+--------------------------+

Case where we have a string matrix

: A
1     2     3     4     5
+-------------------------------+
1 |  a11   a12   a13   a14   a15  |
2 |  a21   a22   a23   a24   a25  |
3 |  a31   a32   a33   a34   a35  |
+-------------------------------+

: mz_reshape(A, 2, 6)
1     2     3     4     5     6
+-------------------------------------+
1 |  a11   a12   a13   a14   a15   a21  |
2 |  a22   a23   a24   a25   a31   a32  |
+-------------------------------------+

: mz_reshape(A, 4, 7)
1     2     3     4     5     6     7
+-------------------------------------------+
1 |  a11   a12   a13   a14   a15   a21   a22  |
2 |  a23   a24   a25   a31   a32   a33   a34  |
3 |  a35   a11   a12   a13   a14   a15   a21  |
4 |  a22   a23   a24   a25   a31   a32   a33  |
+-------------------------------------------+

Conformability

mz_reshape(x, m, n)
x:  r x c
m:  1 x 1
n:  1 x 1
result:  m x n.

Diagnostics

None

Source code

mz_reshape.mata

Author