[SciPy-User] Rerranging an array
Christopher Barker
Chris.Barker@noaa....
Tue Nov 9 15:38:42 CST 2010
On 11/9/10 12:27 PM, Jose Gomez-Dans wrote:
> another array with the following setup
> [ id_number, observation]. The id_number refer to the first array, and
> indicates the position where observation goes.
Is there a structure to that array? Are the id_numbers in order? if so,
then you may be able to simply re-shape it.
If not, then maybe something like:
(see "fancy indexing")
>>> a2 = np.array(((3,45),(2,15),(4,65)))
>>> a2
array([[ 3, 45],
[ 2, 15],
[ 4, 65]])
# an empty 2-d array:
>>> a = np.zeros((3,4))
# reshape it to 1-d
>>> a.shape = ((-1,))
# assign the values by id_number
>>> a[a2[:,0]] = a2[:,1]
# make it 2-d again
>>> a.shape = (3,4)
>>> a
array([[ 0., 0., 15., 45.],
[ 65., 0., 0., 0.],
[ 0., 0., 0., 0.]])
- Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov
More information about the SciPy-User
mailing list