# [Numpy-discussion] Matrix to Array Transformation assistance required

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Apr 4 00:04:13 EDT 2011

```On Sun, Apr 3, 2011 at 8:03 PM, David Crisp <david.crisp at gmail.com> wrote:
> Hello,
>
> I am trying to convert a numpy matrix to an array containing
> coordinate pairs,  and I am having difficulty understanding the
> transformation process.   I know there is a fairly simply way of doing
> this transformation but am unsure of the commands to use.
>
> The matrix I have is created with the following command
>
> vegetation_matrix = numpy.empty(shape=(x_bins,y_bins))
>
> x_bins and y_bins are dynamic in size depending on the input data and
> the required precision.   Some tests I need to perform could result in
> a 10 by 10 matrix and some could result in a 4000,5000 matrix,
> However,  these values are dynamic and could be anything.  For
> instance they COULD be 300,124 or 400,900 etc.. these are unknown
> values until the data has been collected.
>
> A sample matrix view will be:
>
>>>> a
> array([['x', 'o', 'o'],
>      ['o', 'x', 'x'],
>      ['o', 'x', 'o']],
>     dtype='|S1')
>
> I wish to convert this to:
> 0,0,x
> 0,1,o
> 0,2,o
> ...
> 2,2,o
>
> I have been given the following example which works on a known size of data.
>
>>>> a
> array([['x', 'o', 'o'],
>      ['o', 'x', 'x'],
>      ['o', 'x', 'o']],
>     dtype='|S1')
>
>>>> numoy.array([numpy.arange(9)//3, numpy.arange(9)%3, a.flatten()]).transpose() array([['0', '0', 'x'],
>      ['0', '1', 'o'],
>      ['0', '2', 'o'],
>      ['1', '0', 'o'],
>      ['1', '1', 'x'],
>      ['1', '2', 'x'],
>      ['2', '0', 'o'],
>      ['2', '1', 'x'],
>      ['2', '2', 'o']],
>     dtype='|S8')
>
> But when I try and convert that line to work on my own matrix with its
> larger size,   I do not understand how to make it work.

It looks to me that you just have to replace the 9 and the 3 with your
2d array size and shape, for example

>>> a = np.arange(12).reshape(3,4)
>>> a
array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11]])
>>> np.column_stack((np.arange(a.size)//a.shape[1], np.arange(a.size)%a.shape[1], a.ravel()))
array([[ 0,  0,  0],
[ 0,  1,  1],
[ 0,  2,  2],
[ 0,  3,  3],
[ 1,  0,  4],
[ 1,  1,  5],
[ 1,  2,  6],
[ 1,  3,  7],
[ 2,  0,  8],
[ 2,  1,  9],
[ 2,  2, 10],
[ 2,  3, 11]])

Or do I misunderstand the question?

Josef

>
> Would somebody be able to provide a little assistance on how I can proceed here.
>
> (I am a little new to python and part of the problem I have is using
> the correct terminology for various processes etc)
>
> Regards,
> David
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

```