Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31344
| Date | 2012-10-15 22:26 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: numpy - 2D matrix/array - initialization like in Matlab... |
| References | <k5hu2e$h5i$1@dont-email.me> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2241.1350336380.27098.python-list@python.org> (permalink) |
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]])
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
numpy - 2D matrix/array - initialization like in Matlab... someone <newsboost@gmail.com> - 2012-10-15 23:09 +0200
Re: numpy - 2D matrix/array - initialization like in Matlab... MRAB <python@mrabarnett.plus.com> - 2012-10-15 22:26 +0100
Re: numpy - 2D matrix/array - initialization like in Matlab... someone <newsboost@gmail.com> - 2012-10-16 00:42 +0200
Re: numpy - 2D matrix/array - initialization like in Matlab... Marco Nawijn <nawijn@gmail.com> - 2012-10-16 03:34 -0700
Re: numpy - 2D matrix/array - initialization like in Matlab... Marco Nawijn <nawijn@gmail.com> - 2012-10-16 03:34 -0700
Re: numpy - 2D matrix/array - initialization like in Matlab... Emile van Sebille <emile@fenx.com> - 2012-10-15 14:52 -0700
csiph-web