[Numpy-discussion] Matrix to Array Transformation assistance required

David Crisp david.crisp at gmail.com
Sun Apr 3 20:03:49 EDT 2011


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']],

I wish to convert this to:

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']],

>>> 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']],

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.

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)


More information about the NumPy-Discussion mailing list