[Numpy-discussion] how to create a matrix based on a vector?
Robert Kern
robert.kern@gmail....
Mon Dec 8 11:30:31 CST 2008
On Mon, Dec 8, 2008 at 11:27, frank wang <f.yw@hotmail.com> wrote:
> Hi,
>
> I want to create a matrix based on a vector. It is difficult to describe the
> issue for me in english. Here is an example.
>
> Suppose I have an array([3, 6, 8, 12]), I want to create a range based on
> each element. In this exampe, let us say want to create 4 number with step
> 2, so I will have
>
> [3, 6, 8, 12
> 5, 8, 10,14
> 7, 10,12,16
> 9, 12,14,18]
>
> It is a 4 by 4 maxtric in this example. My original array is quite large.
> but the range I want to create around the number is not big, it is about 30.
>
> Does anyone know how to do this efficiently?
In [1]: from numpy import *
In [2]: a = array([3, 6, 8, 12])
In [4]: b = arange(0, 4*2, 2)[:,newaxis]
In [5]: a+b
Out[5]:
array([[ 3, 6, 8, 12],
[ 5, 8, 10, 14],
[ 7, 10, 12, 16],
[ 9, 12, 14, 18]])
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion
mailing list