# Data Structure Manipulations in METSIM - 3

Data Structure Manipulations - 3

We have been using using catenate with axis to join conforming matrices. We have seen that the catenation is made in such a way that changes the length of the specified axis. So " M1 , [ 2 ] M2 " joined the two matrices M1 and M2 with M2 added to the right (increasing the number of columns).

For a vector, a matrix, or any other higher order data array the "axis of increasing number of columns" is always the "last", or "rightmost" in the vector of axes. For a vector the increasing columns axis is [ 1 ], for a matrix it is [ 2 ], for a 3D array it is [ 3 ], but it is always the rightmost. By default, if you do not specify an axis for catenation, APL will assume you want to catenate using the rightmost axis, and so " M1 , M2 " works the same as " M1, [ 2 ] M2 " in this example.

But the case with catenating vectors is more interesting. A vector only has one dimension. "V1 , [ 1 ] V2 " will be OK, and the values in V2 will be appended to the right of V1. But how do we catenate two conforming vectors to make a matrix with each vector as rows? There is no "rows" dimension.

It is very easy to change a vector into a single row matrix (reshape it with rho) if you know the vector length and then the M1 , [ 1 ] M2 method above will work, but APL has another interesting variation of the "catenate with axis" that shows how APL makes doing data manipulation easy.

To catenate a vector to be below another conforming vector, we are asking APL to catenate it along a non existing axis - which seems reasonable as long as we ask it to create the required dimension while we are at it. A vector only has one axis, and of course it is the rightmost of the vector of one value. If we want to insert a dimension for rows, we will want to add that dimension length to the left of the columns dimension length in the shape vector.

So if the increasing number of columns axis is [ 1 ] then we will want an axis specification that is "before" [ 1 ]. APL will accept any fraction between 0 and 1 as an axis specification as being a way of saying "before [ 1 ] " So " V1 , [ 0.5 ] V2 " will insert a "rows" dimension in the shape vector of V1 (with the appropriate length) and will then catenate to increase the length of the "up rounded integer" specified axis. Note any non zero fraction between 0 and 1 will up round to 1, so APL sees the fractional specification as being a request to create the required dimension and knows where you want it added (or inserted).

This extends to specifications like " V1 , [ 1.1 ] V2 " and the "with axis" specification applies to other structural transformation operators as well. If my explanation doesn't work for you try "APL2 At A Glance". Mine is more long winded, repeating the words that I missed the significance of until later, more for an APL beginner than someone who already thinks APL.

Cheers, Jim