[Numpy-discussion] Re: What am I missing about concatenate?

Tim Hochberg tim.hochberg at cox.net
Thu Jun 1 12:59:02 EDT 2006


Christopher Barker wrote:

> Thanks all,
>
>
> Robert Kern wrote:
>
>> Look at vstack() (and also its friends hstack(), dstack() and 
>> column_stack() for
>> completeness).
>
>
> I like this, but need to keep Numeric/numarray compatibility for the 
> moment -- I think, I've just sent out a query to my users.
>
>
>
> Tim Hochberg wrote:
>
>> If you are using real arrays, use newaxis:
>>
>>     >>> a
>>    array([0, 1, 2])
>>     >>> b
>>    array([3, 4, 5])
>>     >>> concatenate([a[newaxis], b[newaxis]], 0)
>>    array([[0, 1, 2],
>>           [3, 4, 5]])
>
>
> I like this, but again, not in Numeric -- I really need to dump that 
> as soon as I can!


In Numeric, you can use NewAxis instead for the same effect.

>
>> hate newaxis, wrap the arrays in [] to give them an extra dimension. 
>> This tends to look nicer, but I suspect has poorer performance than 
>> above (haven't timed it though):
>>
>>     >>> concatenate([[a], [b]], 0)
>>    array([[0, 1, 2],
>>           [3, 4, 5]])
>
>
> Lovely. much cleaner.
>
> By they way, wouldn't wrapping in a tuple, be slightly better, 
> performance-wise (I know, probably negligible, but I always feel that 
> I should use a tuple when I don't need mutability)

I doubt it would make a signifigant difference and the square brackets 
are much easier to read IMO. Your mileage may vary.

-tim









More information about the NumPy-Discussion mailing list