On Thu, Oct 6, 2011 at 7:29 AM, Samuel John wrote:
I just learned two things:

1. np.newaxis
2. Array dimension broadcasting rocks more than you think.

Yup. :)

The x[:, np.newaxis] might not be the most intuitive solution but it's great and powerful.
Intuitive would be to have x.T to transform [0,1,2,4] into [[0],[1],[2],[4]].

I agree, creating a new dimension by indexing with np.newaxis isn't the first thing I would guess if I didn't already know about it.  An alternative is x.reshape(4,1) (or even better, x.reshape(-1,1) so it doesn't explicitly refer to the length of x).

(Also, you probably noticed that transposing won't work, because x is one-dimensional.  The transpose operation simply swaps dimensions, and with just one dimension there is nothing to swap; x.T is the same as x.)

Warren

Thanks Warren :-)
Samuel

On 06.10.2011, at 14:18, Warren Weckesser wrote:

>
>
> On Thu, Oct 6, 2011 at 7:08 AM, Neal Becker <ndbecker2@gmail.com> wrote:
> Given a vector y, I want a matrix H whose rows are
>
> y - x0
> y - x1
> y - x2
> ...
>
>
> where x_i are scalars
>
> Suggestion?
>
>
>
> In [15]: import numpy as np
>
> In [16]: y = np.array([10.0, 20.0, 30.0])
>
> In [17]: x = np.array([0, 1, 2, 4])
>
> In [18]: H = y - x[:, np.newaxis]
>
> In [19]: H
> Out[19]:
> array([[ 10.,  20.,  30.],
>        [  9.,  19.,  29.],
>        [  8.,  18.,  28.],
>        [  6.,  16.,  26.]])
>
>
> Warren
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion