unpack

The unpack operator produces a one-dimensional result array from a multi-dimensional array.

The unpack operator excludes empty cells from the result.

Synopsis

unpack(source_array,dimension_name[,chunk_size]);

Summary

The unpack() operator produces a one-dimensional array from a multi-dimensional array.  The result array has a single zero-based dimension and the same attributes as the input. The user specifies the name for the new single dimension with the second argument.

You can control the chunk size of the resulting array with the optional chunk_size parameter.

Example Application: You could use unpack to save a multidimensional array into a binary file, by performing a binary save on unpacked data.

Examples

Reduce a 2-Dimensional, 2-Attribute Array to One Dimension

To reduce a 2-dimensional, 2-attribute array to a single dimension:

  1. Create a 2-attribute, 2-dimensional array with some example data:

    AFL% store(apply(build(<a:double>[row=1:2; col=1:3],(row-1)*3+col+100),b,a+100),M);


    The output is:

    {row,col} a,b
    {1,1} 101,201
    {1,2} 102,202
    {1,3} 103,203
    {2,1} 104,204
    {2,2} 105,205
    {2,3} 106,206

     

  2. Unpack M into a 1-dimensional array:

    AFL% unpack(M, i);  


    The output is:

    {i} row,col,a,b
    {0} 1,1,101,201
    {1} 1,2,102,202
    {2} 1,3,103,203
    {3} 2,1,104,204
    {4} 2,2,105,205
    {5} 2,3,106,206

    Note that the original dimensions are preserved as attributes row and col.

Using unpack to back up an Array into a Binary File

To back up an array into a binary file using unpack, do the following:

  1. Unpack Names into a 1-dimensional array, namesFlat.

    AFL% store(unpack(M,x),S); 


    The output is:

    {x} row,col,a,b
    {0} 1,1,101,201
    {1} 1,2,102,202
    {2} 1,3,103,203
    {3} 2,1,104,204
    {4} 2,2,105,205
    {5} 2,3,106,206

     

  2. View the schema for S, so you can create the query for the binary save:

    AFL% show(S);


    The output is:

    {i} schema
    {0} 'S<row:int64 NOT NULL,col:int64 NOT NULL,a:double,b:double>[s=0:*:0:6]'
  3. Perform the binary save:

    AFL% save(S,'/tmp/S.dat', 0, '(int64,int64,double,double)'); 


    The output is:

    {i} row,col,a,b

    This writes the binary file, named S.dat, into the /tmp folder on instance 0.

Â