numpy - 2D matrix/array - initialization like in Matlab...

Discussion in 'Python' started by someone, Oct 15, 2012.

  1. someone

    someone Guest

    See this:

    ==========================================================
    In [5]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 1.5')

    In [6]: Dx
    Out[6]:
    matrix([[ 1. , 0. , 0. ],
    [ 0. , 0.5, -0.5],
    [ 0. , -0.5, 1.5]])
    ==========================================================



    Ok... So now test = 33 and instead of the value 1.5 I want to use the
    value of "test" and put it directly into the matrix (or array):

    ==========================================================
    In [7]: test=33

    In [8]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test')
    ---------------------------------------------------------------------------
    NameError Traceback (most recent call last)
    /home/user/something/<ipython-input-8-5a43575649e1> in <module>()
    ----> 1 Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test')

    /usr/lib/python2.7/dist-packages/numpy/matrixlib/defmatrix.pyc in
    __new__(subtype, data, dtype, copy)
    252
    253 if isinstance(data, str):
    --> 254 data = _convert_from_string(data)
    255
    256 # now convert data to an array
    ....... etc...
    ==========================================================



    So obviously it doesn't understand that I want this:

    ==========================================================
    In [21]: Dx[2,2]=test

    In [22]: Dx
    Out[22]:
    matrix([[ 1. , 0. , 0. ],
    [ 0. , 33. , -0.5],
    [ 0. , -0.5, 33. ]])
    ==========================================================

    Without having to manually change all the individual places using my
    variables (test is actually many variables, not just one but I think you
    should understand the problem now).


    How to initialize my array directly using variables ?

    It could also be that I wanted:

    test11 = 1
    test12 = 1.5
    test13 = 2
    test21 = 0
    test22 = 5

    Dx = numpy.matrix('test11 test12 test13; test21 test22 -0.5; 0 -0.5 1.5')

    Etc... for many variables...

    Appreciate ANY help, thank you very much!
    someone, Oct 15, 2012
    #1
    1. Advertising

  2. someone

    MRAB Guest

    On 2012-10-15 22:09, someone wrote:
    >
    > See this:
    >
    > ==========================================================
    > In [5]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 1.5')
    >
    > In [6]: Dx
    > Out[6]:
    > matrix([[ 1. , 0. , 0. ],
    > [ 0. , 0.5, -0.5],
    > [ 0. , -0.5, 1.5]])
    > ==========================================================
    >
    >
    >
    > Ok... So now test = 33 and instead of the value 1.5 I want to use the
    > value of "test" and put it directly into the matrix (or array):
    >
    > ==========================================================
    > In [7]: test=33
    >
    > In [8]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test')
    > ---------------------------------------------------------------------------
    > NameError Traceback (most recent call last)
    > /home/user/something/<ipython-input-8-5a43575649e1> in <module>()
    > ----> 1 Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test')
    >
    > /usr/lib/python2.7/dist-packages/numpy/matrixlib/defmatrix.pyc in
    > __new__(subtype, data, dtype, copy)
    > 252
    > 253 if isinstance(data, str):
    > --> 254 data = _convert_from_string(data)
    > 255
    > 256 # now convert data to an array
    > ...... etc...
    > ==========================================================
    >
    >
    >
    > So obviously it doesn't understand that I want this:
    >
    > ==========================================================
    > In [21]: Dx[2,2]=test
    >
    > In [22]: Dx
    > Out[22]:
    > matrix([[ 1. , 0. , 0. ],
    > [ 0. , 33. , -0.5],
    > [ 0. , -0.5, 33. ]])
    > ==========================================================
    >
    > Without having to manually change all the individual places using my
    > variables (test is actually many variables, not just one but I think you
    > should understand the problem now).
    >
    >
    > How to initialize my array directly using variables ?
    >
    > It could also be that I wanted:
    >
    > test11 = 1
    > test12 = 1.5
    > test13 = 2
    > test21 = 0
    > test22 = 5
    >
    > Dx = numpy.matrix('test11 test12 test13; test21 test22 -0.5; 0 -0.5 1.5')
    >
    > Etc... for many variables...
    >
    > Appreciate ANY help, thank you very much!
    >

    What it prints should give you a hint:

    >>> Dx = numpy.matrix([[test11, test12, test13], [test21, test22,

    -0.5], [0, -0.5, 1.5]])
    >>> Dx

    matrix([[ 1. , 1.5, 2. ],
    [ 0. , 5. , -0.5],
    [ 0. , -0.5, 1.5]])
    MRAB, Oct 15, 2012
    #2
    1. Advertising

  3. someone wrote:
    > How to initialize my array directly using variables ?
    >
    > It could also be that I wanted:
    >
    > test11 = 1
    > test12 = 1.5
    > test13 = 2
    > test21 = 0
    > test22 = 5
    >
    > Dx = numpy.matrix('test11 test12 test13; test21 test22 -0.5; 0 -0.5 1.5')
    >
    > Etc... for many variables...
    >
    > Appreciate ANY help, thank you very much!


    You could use string interpolation:

    Dx = numpy.matrix('%s %s %s; %s %s -0.5; 0 -0.5 1.5'
    % (test11 test12 test13 test21 test22))


    Emile
    Emile van Sebille, Oct 15, 2012
    #3
  4. someone

    someone Guest

    On 10/15/2012 11:26 PM, MRAB wrote:
    > On 2012-10-15 22:09, someone wrote:
    >>
    >> See this:
    >>
    >> ==========================================================
    >> In [5]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 1.5')
    >>
    >> In [6]: Dx
    >> Out[6]:
    >> matrix([[ 1. , 0. , 0. ],
    >> [ 0. , 0.5, -0.5],
    >> [ 0. , -0.5, 1.5]])
    >> ==========================================================
    >>
    >>
    >>
    >> Ok... So now test = 33 and instead of the value 1.5 I want to use the
    >> value of "test" and put it directly into the matrix (or array):
    >>
    >> ==========================================================
    >> In [7]: test=33
    >>
    >> In [8]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test')
    >> ---------------------------------------------------------------------------
    >>
    >> NameError Traceback (most recent call
    >> last)
    >> /home/user/something/<ipython-input-8-5a43575649e1> in <module>()
    >> ----> 1 Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test')
    >>
    >> /usr/lib/python2.7/dist-packages/numpy/matrixlib/defmatrix.pyc in
    >> __new__(subtype, data, dtype, copy)
    >> 252
    >> 253 if isinstance(data, str):
    >> --> 254 data = _convert_from_string(data)
    >> 255
    >> 256 # now convert data to an array
    >> ...... etc...
    >> ==========================================================
    >>
    >>
    >>
    >> So obviously it doesn't understand that I want this:
    >>
    >> ==========================================================
    >> In [21]: Dx[2,2]=test
    >>
    >> In [22]: Dx
    >> Out[22]:
    >> matrix([[ 1. , 0. , 0. ],
    >> [ 0. , 33. , -0.5],
    >> [ 0. , -0.5, 33. ]])
    >> ==========================================================
    >>
    >> Without having to manually change all the individual places using my
    >> variables (test is actually many variables, not just one but I think you
    >> should understand the problem now).
    >>
    >>
    >> How to initialize my array directly using variables ?
    >>
    >> It could also be that I wanted:
    >>
    >> test11 = 1
    >> test12 = 1.5
    >> test13 = 2
    >> test21 = 0
    >> test22 = 5
    >>
    >> Dx = numpy.matrix('test11 test12 test13; test21 test22 -0.5; 0 -0.5 1.5')
    >>
    >> Etc... for many variables...
    >>
    >> Appreciate ANY help, thank you very much!
    >>

    > What it prints should give you a hint:
    >
    > >>> Dx = numpy.matrix([[test11, test12, test13], [test21, test22,

    > -0.5], [0, -0.5, 1.5]])
    > >>> Dx

    > matrix([[ 1. , 1.5, 2. ],
    > [ 0. , 5. , -0.5],
    > [ 0. , -0.5, 1.5]])


    Uh, great - thank you very much!

    As you maybe see, I'm only a python newbie so I'm not so good at
    understanding the error messages and reading the source code yet.

    Thank you very much for the solution to the problem! It's highly
    appreciated. Thanks.
    someone, Oct 15, 2012
    #4
  5. someone

    Marco Nawijn Guest

    On Tuesday, October 16, 2012 12:43:09 AM UTC+2, someone wrote:
    > On 10/15/2012 11:26 PM, MRAB wrote:
    >
    > > On 2012-10-15 22:09, someone wrote:

    >
    > >>

    >
    > >> See this:

    >
    > >>

    >
    > >> ==========================================================

    >
    > >> In [5]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 1.5')

    >
    > >>

    >
    > >> In [6]: Dx

    >
    > >> Out[6]:

    >
    > >> matrix([[ 1. , 0. , 0. ],

    >
    > >> [ 0. , 0.5, -0.5],

    >
    > >> [ 0. , -0.5, 1.5]])

    >
    > >> ==========================================================

    >
    > >>

    >
    > >>

    >
    > >>

    >
    > >> Ok... So now test = 33 and instead of the value 1.5 I want to use the

    >
    > >> value of "test" and put it directly into the matrix (or array):

    >
    > >>

    >
    > >> ==========================================================

    >
    > >> In [7]: test=33

    >
    > >>

    >
    > >> In [8]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test')

    >
    > >> ---------------------------------------------------------------------------

    >
    > >>

    >
    > >> NameError Traceback (most recent call

    >
    > >> last)

    >
    > >> /home/user/something/<ipython-input-8-5a43575649e1> in <module>()

    >
    > >> ----> 1 Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test')

    >
    > >>

    >
    > >> /usr/lib/python2.7/dist-packages/numpy/matrixlib/defmatrix.pyc in

    >
    > >> __new__(subtype, data, dtype, copy)

    >
    > >> 252

    >
    > >> 253 if isinstance(data, str):

    >
    > >> --> 254 data = _convert_from_string(data)

    >
    > >> 255

    >
    > >> 256 # now convert data to an array

    >
    > >> ...... etc...

    >
    > >> ==========================================================

    >
    > >>

    >
    > >>

    >
    > >>

    >
    > >> So obviously it doesn't understand that I want this:

    >
    > >>

    >
    > >> ==========================================================

    >
    > >> In [21]: Dx[2,2]=test

    >
    > >>

    >
    > >> In [22]: Dx

    >
    > >> Out[22]:

    >
    > >> matrix([[ 1. , 0. , 0. ],

    >
    > >> [ 0. , 33. , -0.5],

    >
    > >> [ 0. , -0.5, 33. ]])

    >
    > >> ==========================================================

    >
    > >>

    >
    > >> Without having to manually change all the individual places using my

    >
    > >> variables (test is actually many variables, not just one but I think you

    >
    > >> should understand the problem now).

    >
    > >>

    >
    > >>

    >
    > >> How to initialize my array directly using variables ?

    >
    > >>

    >
    > >> It could also be that I wanted:

    >
    > >>

    >
    > >> test11 = 1

    >
    > >> test12 = 1.5

    >
    > >> test13 = 2

    >
    > >> test21 = 0

    >
    > >> test22 = 5

    >
    > >>

    >
    > >> Dx = numpy.matrix('test11 test12 test13; test21 test22 -0.5; 0 -0.5 1.5')

    >
    > >>

    >
    > >> Etc... for many variables...

    >
    > >>

    >
    > >> Appreciate ANY help, thank you very much!

    >
    > >>

    >
    > > What it prints should give you a hint:

    >
    > >

    >
    > > >>> Dx = numpy.matrix([[test11, test12, test13], [test21, test22,

    >
    > > -0.5], [0, -0.5, 1.5]])

    >
    > > >>> Dx

    >
    > > matrix([[ 1. , 1.5, 2. ],

    >
    > > [ 0. , 5. , -0.5],

    >
    > > [ 0. , -0.5, 1.5]])

    >
    >
    >
    > Uh, great - thank you very much!
    >
    >
    >
    > As you maybe see, I'm only a python newbie so I'm not so good at
    >
    > understanding the error messages and reading the source code yet.
    >
    >
    >
    > Thank you very much for the solution to the problem! It's highly
    >
    > appreciated. Thanks.


    Hi,

    Also note that you don't need to initialize the array with a string. You could directly do it like this:

    >>> a = numpy.array(((1,2,3), (2,3,4), (4,5,6)))


    Other things that might be interesting for you are:

    # List comprehension (standard python) to convert strings to floats
    >>> vals = [ float(s) for s in "1.0 2.3 1.2".split() ]

    produces [1.0, 2.3, 1.2]
    >>> vals = [ float(s) for s in ("1.0", "2.3", "1.2") ]

    produces again [1.0, 2.3, 1.2]

    Also lookup the documentation for numpy.reshape. With this you could provide a single list of for example 9 numbers and reshape it into a 3x3 array.

    Python and Numpy are so cool!!

    Marco
    Marco Nawijn, Oct 16, 2012
    #5
  6. someone

    Marco Nawijn Guest

    On Tuesday, October 16, 2012 12:43:09 AM UTC+2, someone wrote:
    > On 10/15/2012 11:26 PM, MRAB wrote:
    >
    > > On 2012-10-15 22:09, someone wrote:

    >
    > >>

    >
    > >> See this:

    >
    > >>

    >
    > >> ==========================================================

    >
    > >> In [5]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 1.5')

    >
    > >>

    >
    > >> In [6]: Dx

    >
    > >> Out[6]:

    >
    > >> matrix([[ 1. , 0. , 0. ],

    >
    > >> [ 0. , 0.5, -0.5],

    >
    > >> [ 0. , -0.5, 1.5]])

    >
    > >> ==========================================================

    >
    > >>

    >
    > >>

    >
    > >>

    >
    > >> Ok... So now test = 33 and instead of the value 1.5 I want to use the

    >
    > >> value of "test" and put it directly into the matrix (or array):

    >
    > >>

    >
    > >> ==========================================================

    >
    > >> In [7]: test=33

    >
    > >>

    >
    > >> In [8]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test')

    >
    > >> ---------------------------------------------------------------------------

    >
    > >>

    >
    > >> NameError Traceback (most recent call

    >
    > >> last)

    >
    > >> /home/user/something/<ipython-input-8-5a43575649e1> in <module>()

    >
    > >> ----> 1 Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test')

    >
    > >>

    >
    > >> /usr/lib/python2.7/dist-packages/numpy/matrixlib/defmatrix.pyc in

    >
    > >> __new__(subtype, data, dtype, copy)

    >
    > >> 252

    >
    > >> 253 if isinstance(data, str):

    >
    > >> --> 254 data = _convert_from_string(data)

    >
    > >> 255

    >
    > >> 256 # now convert data to an array

    >
    > >> ...... etc...

    >
    > >> ==========================================================

    >
    > >>

    >
    > >>

    >
    > >>

    >
    > >> So obviously it doesn't understand that I want this:

    >
    > >>

    >
    > >> ==========================================================

    >
    > >> In [21]: Dx[2,2]=test

    >
    > >>

    >
    > >> In [22]: Dx

    >
    > >> Out[22]:

    >
    > >> matrix([[ 1. , 0. , 0. ],

    >
    > >> [ 0. , 33. , -0.5],

    >
    > >> [ 0. , -0.5, 33. ]])

    >
    > >> ==========================================================

    >
    > >>

    >
    > >> Without having to manually change all the individual places using my

    >
    > >> variables (test is actually many variables, not just one but I think you

    >
    > >> should understand the problem now).

    >
    > >>

    >
    > >>

    >
    > >> How to initialize my array directly using variables ?

    >
    > >>

    >
    > >> It could also be that I wanted:

    >
    > >>

    >
    > >> test11 = 1

    >
    > >> test12 = 1.5

    >
    > >> test13 = 2

    >
    > >> test21 = 0

    >
    > >> test22 = 5

    >
    > >>

    >
    > >> Dx = numpy.matrix('test11 test12 test13; test21 test22 -0.5; 0 -0.5 1.5')

    >
    > >>

    >
    > >> Etc... for many variables...

    >
    > >>

    >
    > >> Appreciate ANY help, thank you very much!

    >
    > >>

    >
    > > What it prints should give you a hint:

    >
    > >

    >
    > > >>> Dx = numpy.matrix([[test11, test12, test13], [test21, test22,

    >
    > > -0.5], [0, -0.5, 1.5]])

    >
    > > >>> Dx

    >
    > > matrix([[ 1. , 1.5, 2. ],

    >
    > > [ 0. , 5. , -0.5],

    >
    > > [ 0. , -0.5, 1.5]])

    >
    >
    >
    > Uh, great - thank you very much!
    >
    >
    >
    > As you maybe see, I'm only a python newbie so I'm not so good at
    >
    > understanding the error messages and reading the source code yet.
    >
    >
    >
    > Thank you very much for the solution to the problem! It's highly
    >
    > appreciated. Thanks.


    Hi,

    Also note that you don't need to initialize the array with a string. You could directly do it like this:

    >>> a = numpy.array(((1,2,3), (2,3,4), (4,5,6)))


    Other things that might be interesting for you are:

    # List comprehension (standard python) to convert strings to floats
    >>> vals = [ float(s) for s in "1.0 2.3 1.2".split() ]

    produces [1.0, 2.3, 1.2]
    >>> vals = [ float(s) for s in ("1.0", "2.3", "1.2") ]

    produces again [1.0, 2.3, 1.2]

    Also lookup the documentation for numpy.reshape. With this you could provide a single list of for example 9 numbers and reshape it into a 3x3 array.

    Python and Numpy are so cool!!

    Marco
    Marco Nawijn, Oct 16, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. siki
    Replies:
    0
    Views:
    954
  2. Replies:
    2
    Views:
    486
    Robert Kern
    Nov 13, 2007
  3. Luna Moon
    Replies:
    16
    Views:
    1,334
    Giovanni Gherdovich
    Aug 8, 2008
  4. Matt Funk

    numpy/matlab compatibility

    Matt Funk, Jan 25, 2011, in forum: Python
    Replies:
    1
    Views:
    188
    Steven D'Aprano
    Jan 25, 2011
  5. someone
    Replies:
    39
    Views:
    1,452
    someone
    May 4, 2012
Loading...

Share This Page