2
$\begingroup$

Printing photo matrices (x,y,z) where z is 0:2 colour, I want to see (x in rows, y in columns) 3 times, once for each colour (the way I've been taught by every mathematician). By default, numpy does it last dimension first.

$\endgroup$
2
  • $\begingroup$ As in print A.T? $\endgroup$
    – Nat Wilson
    Commented Mar 29, 2013 at 23:46
  • $\begingroup$ maybe this is a stackoverflow question...? $\endgroup$
    – imbr
    Commented Sep 5, 2013 at 13:11

1 Answer 1

2
$\begingroup$

You can use array slicing.

So if you create a NumPy array (ndarray, to be precise), it might look like this:

a = numpy.arange(27).reshape(3,3,3)

And if you do print( a[:,:,:] ) it will print this out:

array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])

And so you can do things like print( a[:,:,0] ) you get:

array([[ 0,  3,  6],
       [ 9, 12, 15],
       [18, 21, 24]])

Try experimenting with a[0,:,:] or a[:,0,:] to see if that's what you want.

Note that these are views on to the array, so if you want to store it somehow you should copy it into a new array.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.