Now that we’ve learned how to create arrays and apply simple operations to them, lets explore how we can re-order and modify them. Arrayfire offers several functions that can easily reformat the structures of arrays.
<h5><a id="These_functions_are_2"></a>These functions are:</h5>
<ul>
	<li><strong>flat()</strong> - flatten an array to one dimension</li>
	<li><strong>flip()</strong> - flip an array along a dimension</li>
	<li><strong>join()</strong> - join up to 4 arrays</li>
	<li><strong>moddims()</strong> - change the dimensions of an array without changing the data</li>
	<li><strong>reorder()</strong> - changes the dimension order within the array</li>
	<li><strong>shift()</strong> - shifts data along a dimension</li>
	<li><strong>tile()</strong> - repeats an array along a dimension</li>
	<li><strong>transpose()</strong> - performs a matrix transpose</li>
</ul>
Below, are several examples showing the details of each function. We’ll start by defining an example matrix:
<h3><a id="flat_15"></a>flat()</h3>
<p>The <strong>flat()</strong> function flattens an array to one dimension.</p>
<pre><code>a [3 3 1 1]
    1.0000     4.0000     7.0000
    2.0000     5.0000     8.0000
    3.0000     6.0000     9.0000

flat(a) [9 1 1 1]
    1.0000
    2.0000
    3.0000
    4.0000
    5.0000
    6.0000
    7.0000
    8.0000
    9.0000</code></pre>
<p>The flat function has the following overloads:</p>
<ul>
<li><strong>array af::flat(const array&amp; in)</strong> – flatten an array</li>
<li><strong>af_err af_flat(af_array* out, const af_array in)</strong> – C interface for flat() function</li>
</ul>
<h3><a id="flip_37"></a>flip()</h3>
<p>The <strong>flip()</strong> function flips the contents of an array along a chosen dimension.</p>
<pre><code>a [5 2 1 1]
    1.0000     6.0000
    2.0000     7.0000
    3.0000     8.0000
    4.0000     9.0000
    5.0000    10.0000

flip(a, 0) [5 2 1 1]
    5.0000    10.0000
    4.0000     9.0000
    3.0000     8.0000
    2.0000     7.0000
    1.0000     6.0000

flip(a, 1) [5 2 1 1]
    6.0000     1.0000
    7.0000     2.0000
    8.0000     3.0000
    9.0000     4.0000
   10.0000     5.0000</code></pre>
<p>The flip function has the following overloads:</p>
<ul>
<li><strong>array af::flip(const array &amp;in, const unsigned dim)</strong> – flips an array along a dimension</li>
<li><strong>af_err af_flip(af_array *out, const af_array in, const unsigned dim)</strong> – C interface for flip()</li>
</ul>
<h3><a id="join_65"></a>join()</h3>
<p>The <strong>join()</strong> function can join up to 4 arrays together.</p>
<pre><code>a [5 1 1 1]
    1.0000
    2.0000
    3.0000
    4.0000
    5.0000

join(0, a, a) [10 1 1 1]
    1.0000
    2.0000
    3.0000
    4.0000
    5.0000
    1.0000
    2.0000
    3.0000
    4.0000
    5.0000

join(1, a, a) [5 2 1 1]
    1.0000     1.0000
    2.0000     2.0000
    3.0000     3.0000
    4.0000     4.0000
    5.0000     5.0000</code></pre>
<p>The join function has several overloads:</p>
<ul>
<li>
<p><strong>array af::join(const int dim, const array &amp;first, const array &amp;second)</strong> – Joins 2 arrays along a dimension</p>
</li>
<li>
<p><strong>array af::join(const int dim, const array &amp;first, const array &amp;second, const array &amp;third)</strong> – Joins 3 arrays along a dimension.</p>
</li>
<li>
<p><strong>array af::join(const int dim, const array &amp;first, const array &amp;second, const array &amp;third, const array &amp;fourth)</strong> – Joins 4 arrays along a dimension</p>
</li>
<li>
<p><strong>af_err af_join(af_array *out, const int dim, const af_array first, const af_array second)</strong> – C interface function to join 2 arrays along a dimension</p>
</li>
<li>
<p><strong>af_err af_join_many(af_array *out, const int dim, const unsigned n_arrays, const af_array *inputs)</strong> – C interface function to join up to 10 arrays along a dimension</p>
</li>
</ul>
<h3><a id="moddims_105"></a>moddims()</h3>
<p>The <strong>moddims()</strong> function changes the dimensions of an array without changing its data or order. It is important to remember that the function only modifies the <em>metadata</em> associated with the array and does not actually modify the content of the array.</p>
<pre><code>a [8 1 1 1]
    1.0000
    2.0000
    1.0000
    2.0000
    1.0000
    2.0000
    1.0000
    2.0000

af::dim4 new_dims(2, 4);
moddims(a, new_dims) [2 4 1 1]
    1.0000     1.0000     1.0000     1.0000
    2.0000     2.0000     2.0000     2.0000

moddims(a, a.elements(), 1, 1, 1) [8 1 1 1]
    1.0000
    2.0000
    1.0000
    2.0000
    1.0000
    2.0000
    1.0000
    2.0000</code></pre>
<p>The moddims function has several overloads:</p>
<ul>
<li><strong>array af::moddims(const array &amp;in, const unsigned ndims, const dim_t *const dims)</strong> – mods number of dimensions to match <em>ndims</em> as specidied in the array <em>dims</em></li>
<li><strong>array af::moddims(const array &amp;in, const dim4 &amp;dims)</strong> – mods dimensions as specified by <em>dims</em></li>
<li><strong>array af::moddims(const array &amp;in, const dim_t d0, const dim_t d1=1, const dim_t d2=1, const dim_t d3=1)</strong> – mods dimensions of an array</li>
<li><strong>af_err af_moddims(af_array *out, const af_array in, const unsigned ndims, const dim_t *const dims)</strong> – C interface to mod dimensions of an array</li>
</ul>
<h3><a id="reorder_139"></a>reorder()</h3>
<p>The <strong>reorder()</strong> function changes the order of the dimensions within the array. This actually alters the underlying data of the array.</p>
<pre><code>a [2 2 3 1]
    1.0000     3.0000
    2.0000     4.0000

    1.0000     3.0000
    2.0000     4.0000

    1.0000     3.0000
    2.0000     4.0000


reorder(a, 1, 0, 2) [2 2 3 1]  //equivalent to a transpose
    1.0000     2.0000
    3.0000     4.0000

    1.0000     2.0000
    3.0000     4.0000

    1.0000     2.0000
    3.0000     4.0000


reorder(a, 2, 0, 1) [3 2 2 1]
    1.0000     2.0000
    1.0000     2.0000
    1.0000     2.0000

    3.0000     4.0000
    3.0000     4.0000
    3.0000     4.0000</code></pre>
<p>The reorder function the following several overloads:</p>
<ul>
<li>
<p><strong>array af::reorder(const array &amp;in, const unsigned x, const unsigned y=1, const unsigned z=2, const unsigned w=3)</strong> – Reorders dimensions of an array</p>
</li>
<li>
<p><strong>af_err af_reorder(af_array *out, const af_array in, const unsigned x, const unsigned y, const unsigned z, const unsigned w)</strong> – C interface for reordering function</p>
</li>
</ul>
<h3><a id="shift_178"></a>shift()</h3>
<p>The <strong>shift()</strong> function shifts data in a circular buffer fashion along a chosen dimension.</p>
<pre><code>a [3 5 1 1]
    0.0000     0.0000     0.0000     0.0000     0.0000
    3.0000     4.0000     5.0000     1.0000     2.0000
    3.0000     4.0000     5.0000     1.0000     2.0000

shift(a, 0, 2 ) [3 5 1 1]
    0.0000     0.0000     0.0000     0.0000     0.0000
    1.0000     2.0000     3.0000     4.0000     5.0000
    1.0000     2.0000     3.0000     4.0000     5.0000

shift(a, -1, 2 ) [3 5 1 1]
    1.0000     2.0000     3.0000     4.0000     5.0000
    1.0000     2.0000     3.0000     4.0000     5.0000
    0.0000     0.0000     0.0000     0.0000     0.0000</code></pre>
<p>The shift function has the following overloads:</p>
<ul>
<li>
<p><strong>array af::shift(const array &amp;in, const int x, const int y=0, const int z=0, const int w=0)</strong> – Shifts array along specified dimensions</p>
</li>
<li>
<p><strong>af_err af_shift(af_array *out, const af_array in, const int x, const int y, const int z, const int w)</strong> – C interface for shifting an array</p>
</li>
</ul>
<h3><a id="tile_201"></a>tile()</h3>
<p>The <strong>tile()</strong> function repeats an array along a dimension</p>
<pre><code>a [3 1 1 1]
    1.0000
    2.0000
    3.0000

tile(a, 2) [6 1 1 1]
    1.0000
    2.0000
    3.0000
    1.0000
    2.0000
    3.0000

tile(a, 2, 2) [6 2 1 1]
    1.0000     1.0000
    2.0000     2.0000
    3.0000     3.0000
    1.0000     1.0000
    2.0000     2.0000
    3.0000     3.0000

af::dim4 tile_dims(1, 2, 3);
tile(a, tile_dims) [3 2 3 1]
    1.0000     1.0000
    2.0000     2.0000
    3.0000     3.0000

    1.0000     1.0000
    2.0000     2.0000
    3.0000     3.0000

    1.0000     1.0000
    2.0000     2.0000
    3.0000     3.0000</code></pre>
<p>The tile function has several overloads:</p>
<ul>
<li><strong>array af::tile(const array &amp;in, const unsigned x, const unsigned y=1, const unsigned z=1, const unsigned w=1)</strong>  –  Tiles array along specified dimensions</li>
<li><strong>array af::tile(const array &amp;in, const dim4 &amp;dims)</strong>  –  Tile an array according to a dim4 object</li>
<li><strong>af_err af_tile(af_array *out, const af_array in, const unsigned x, const unsigned y, const unsigned z, const unsigned w)</strong>  –  C interface for tiling an array</li>
</ul>
<h3><a id="transpose_245"></a>transpose()</h3>
<p>The <strong>transpose()</strong> function performs a standard matrix transpose. The input array must have the dimensions of a 2D-matrix.</p>
<pre><code>a [3 3 1 1]
    1.0000     3.0000     3.0000
    2.0000     1.0000     3.0000
    2.0000     2.0000     1.0000

transpose(a) [3 3 1 1]
    1.0000     2.0000     2.0000
    3.0000     1.0000     2.0000
    3.0000     3.0000     1.0000</code></pre>
<p>The transpose function has several overloads:</p>
<ul>
<li>
<p><strong>array af::transpose(const array &amp;in, const bool conjugate=false)</strong> – Transposes a matrix.</p>
</li>
<li>
<p><strong>void af::transposeInPlace(array &amp;in, const bool conjugate=false)</strong> – Transposes a matrix in-place.</p>
</li>
<li>
<p><strong>af_err af_transpose(af_array *out, af_array in, const bool conjugate)</strong> – C interface to transpose a matrix.</p>
</li>
<li>
<p><strong>af_err af_transpose_inplace(af_array in, const bool conjugate)</strong> – C interface to transpose a matrix in-place.</p>
</li>
</ul>
<p>[array()](\ref af::array) can be used to create a (shallow) copy of a matrix
with different dimensions.  The number of elements must remain the same as
the original array.</p>
<p>\snippet test/matrix_manipulation.cpp ex_matrix_manipulation_moddims</p>
<p>The [T()](\ref af::array::T) and [H()](\ref af::array::H) methods can be
used to form the [matrix or vector transpose](\ref af::array::T) .</p>
<p>\snippet test/matrix_manipulation.cpp ex_matrix_manipulation_transpose</p>
<h3><a id="Combining_reordering_functions_to_enumerate_grid_coordinates_279"></a>Combining re-ordering functions to enumerate grid coordinates</h3>
<p>By using a combination of the array restructuring functions, we can quickly code complex manipulation patterns with a few lines of code. For example, consider generating <em>(x,y)</em> coordinates for a grid where each axis goes from <em>1 to n</em>. Instead of using several loops to populate our arrays we can just use a small combination of the above functions.</p>
<pre><code><span class="hljs-keyword">unsigned</span> n=<span class="hljs-number">3</span>;
af::<span class="hljs-built_in">array</span> xy = join(<span class="hljs-number">1</span>
                tile(seq(<span class="hljs-number">1</span>, n), n)
                flat( transpose(tile(seq(<span class="hljs-number">1</span>, n), <span class="hljs-number">1</span>, n)) )
                   );
xy [<span class="hljs-number">9</span> <span class="hljs-number">2</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span>]
    <span class="hljs-number">1.0000</span>     <span class="hljs-number">1.0000</span>
    <span class="hljs-number">2.0000</span>     <span class="hljs-number">1.0000</span>
    <span class="hljs-number">3.0000</span>     <span class="hljs-number">1.0000</span>
    <span class="hljs-number">1.0000</span>     <span class="hljs-number">2.0000</span>
    <span class="hljs-number">2.0000</span>     <span class="hljs-number">2.0000</span>
    <span class="hljs-number">3.0000</span>     <span class="hljs-number">2.0000</span>
    <span class="hljs-number">1.0000</span>     <span class="hljs-number">3.0000</span>
    <span class="hljs-number">2.0000</span>     <span class="hljs-number">3.0000</span>
    <span class="hljs-number">3.0000</span>     <span class="hljs-number">3.0000</span>
</code></pre>
<h3><a id="Conclusion_298"></a>Conclusion</h3>
<p>Functions provided by arrayfire offer ease and flexibility for efficiently manipulating the structure of arrays. The provided functions can be used as building blocks to generate, shift, or prepare data to any form imaginable!</p>

