[Numpy-discussion] Apply a vector function to each row of a matrix

Neal Becker ndbecker2 at gmail.com
Thu Oct 9 14:48:57 EDT 2008


David Huard wrote:

> On Thu, Oct 9, 2008 at 9:40 AM, Neal Becker <ndbecker2 at gmail.com> wrote:
> 
>> David Huard wrote:
>>
>> > Neal,
>> >
>> > Look at: apply_along_axis
>> >
>> >
>> I guess it'd be:
>>
>> b = empty_like(a)
>> for row in a.shape[0]:
>>  b[row,:] = apply_along_axis (func, row, a)
>>
> 
>> I don't suppose there is a way to do this without explicitly writing a
>> loop.
> 
> 
> Have you tried
> 
> b = apply_along_axis(func, 1, a)
> 
> It should work.
> 
Yes, thanks.

The doc for apply_along_axis is not clear.

For one thing, it says:
The output array. The shape of outarr depends on the return value of func1d. If it returns arrays with the same shape as the input arrays it receives, outarr has the same shape as arr.

What happens if the 'if' clause is not true?





More information about the NumPy-Discussion mailing list