[Numpy-discussion] simple vector->matrix question

Samuel John scipy@samueljohn...
Thu Oct 6 07:29:14 CDT 2011


I just learned two things:

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


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]].

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



More information about the NumPy-Discussion mailing list