[Tutor] Converting a numpy matrix to a numpy array

Steven D'Aprano steve at pearwood.info
Thu Mar 31 16:25:19 CEST 2011


David Crisp wrote:

> I have a 2d matrix representing the X the Y and the Z value of a
> point.  I wish to convert that matrix to an array.    What is a good
> way of doing so?
> 
> Eg:
> Matrix
>  012345
> 0xooooo
> 1xooooo
> 2oxxxxx
> 3oooooo
> 4ooooox
> 5ooooox

It's not clear what this matrix actually is. Is this from a text file? 
What are the X's and O's (letter o)? Are they meant to be place-holders 
for something else, or literally letter X and letter O?

If placeholders, what are they placeholders for? And if literally X and 
O, what are you expecting to do with them in numpy, which expects 
numeric data?


> I want to convert that to a 2d array which looks like:
> 0,0,x
> 0,1,o
> 0,2,o
> 0,3,o
> 0,4,o
> 0,5,o
> .......
> 5,4,o
> 5,5,o

What you are describing is not a two dimensional array. It is a 
one-dimensional list, each item of which includes 2D coordinates as 
explicit data. You almost certainly do not want that! Working with 
numpy, you want to use a numpy two dimensional array that looks 
something like this:

[ [ x o o o o o ]
   [ x o o o o o ]
   [ o x x x x x ]
   [ o o o o o o ]
   [ o o o o o x ]
   [ o o o o o x ] ]

where the coordinates are implied. I have borrowed Python list syntax [] 
for the array. Each row is a [...] and the columns are read down the rows.

Unfortunately, I'm not using my usual computer, so I don't have access 
to numpy at the moment. More detail will have to follow later.




Regards,


-- 
Steven



More information about the Tutor mailing list