Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'string.': 0.04; 'initialize': 0.05; 'mrab': 0.05; 'much!': 0.05; 'newbie': 0.05; 'python)': 0.05; 'python': 0.09; '2.3,': 0.09; '[0,': 0.09; 'array.': 0.09; 'lookup': 0.09; 'str):': 0.09; 'to:addr:comp.lang.python': 0.09; 'variables,': 0.09; 'cc:addr :python-list': 0.10; 'yet.': 0.13; '"test"': 0.16; '(test': 0.16; '1.5,': 0.16; '252': 0.16; '255': 0.16; ']])': 0.16; 'etc...': 0.16; 'hint:': 0.16; 'last)': 0.16; 'nameerror': 0.16; 'numpy': 0.16; 'ok...': 0.16; 'problem!': 0.16; 'subject:array': 0.16; "test')": 0.16; 'uh,': 0.16; 'vals': 0.16; 'wrote:': 0.17; 'variables': 0.17; 'obviously': 0.18; '>>>': 0.18; '(or': 0.18; 'are:': 0.20; 'thanks.': 0.21; 'produces': 0.22; 'subject:skip:i 10': 0.22; 'skip:_ 20': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'appreciated.': 0.26; '(most': 0.27; '2.3': 0.27; 'see,': 0.27; "doesn't": 0.28; 'subject:/': 0.28; '0.5': 0.29; 'prints': 0.29; 'subject:like': 0.29; 'array': 0.29; 'convert': 0.29; 'skip:_ 10': 0.29; 'source': 0.29; "i'm": 0.29; 'maybe': 0.29; 'error': 0.30; 'code': 0.31; 'help,': 0.32; 'could': 0.32; 'traceback': 0.33; 'problem': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'data,': 0.35; 'pm,': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'test': 0.36; 'should': 0.36; 'thank': 0.36; 'october': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'instead': 0.39; 'think': 0.40; 'skip:u 10': 0.60; 'places': 0.61; 'provide': 0.62; 'subject:...': 0.63; 'skip:n 10': 0.63; 'great': 0.64; '(standard': 0.84 Newsgroups: comp.lang.python Date: Tue, 16 Oct 2012 03:34:45 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=137.17.109.98; posting-account=QNsEXgoAAABu_Avbf026MEK8WO7y27Mt References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 137.17.109.98 MIME-Version: 1.0 Subject: Re: numpy - 2D matrix/array - initialization like in Matlab... From: Marco Nawijn To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 204 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350383688 news.xs4all.nl 6880 [2001:888:2000:d::a6]:49271 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31383 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/ in () > > >> ----> 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