[Numpy-discussion] Silly array question

Christopher Barker Chris.Barker at noaa.gov
Fri Apr 7 13:28:02 CDT 2006


Webb Sprague wrote:
> In R, if you have an Nx2 array of integers, you can use that to index
> an TxS array, yielding a 1xN result.

this seems to work:

 >>> import numpy as N
 >>> I = N.array([[0,0], [1,1], [2,2], [1,1]])
 >>> I
array([[0, 0],
        [1, 1],
        [2, 2],
        [1, 1]])

 >>> M = N. array( [[1, 2, 3, 4], [5, 6, 7, 8], [9,10,11, 12], [13, 14, 
15, 16]])

 >>> M
array([[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12],
        [13, 14, 15, 16]])

 >>> M[I[:,0], I[:,1]]
array([ 1,  6, 11,  6])

-- 
Christopher Barker, Ph.D.
Oceanographer
                                     		
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the Numpy-discussion mailing list