Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; 'initialize': 0.05; 'much!': 0.05; '[0,': 0.09; 'str):': 0.09; 'variables,': 0.09; '"test"': 0.16; '(test': 0.16; '1.5,': 0.16; '252': 0.16; '255': 0.16; ']])': 0.16; 'etc...': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'hint:': 0.16; 'last)': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'nameerror': 0.16; 'ok...': 0.16; 'subject:array': 0.16; "test')": 0.16; 'wrote:': 0.17; 'variables': 0.17; 'obviously': 0.18; '>>>': 0.18; '(or': 0.18; 'subject:skip:i 10': 0.22; 'skip:_ 20': 0.22; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; "doesn't": 0.28; 'subject:/': 0.28; '0.5': 0.29; 'prints': 0.29; 'received:192.168.1.3': 0.29; 'subject:like': 0.29; 'array': 0.29; 'convert': 0.29; 'skip:_ 10': 0.29; 'help,': 0.32; 'could': 0.32; 'traceback': 0.33; 'problem': 0.33; 'to:addr :python-list': 0.33; 'data,': 0.35; 'but': 0.36; 'test': 0.36; 'should': 0.36; 'thank': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'places': 0.61; 'subject:...': 0.63; 'skip:n 10': 0.63; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=OqzNOlDt c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=b2nRVtXOy8EA:10 a=ajOmpfcDGDEA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=mowcf53Ve0AA:10 a=I7TQ57CC7wmf3aqUL2IA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Mon, 15 Oct 2012 22:26:18 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: numpy - 2D matrix/array - initialization like in Matlab... References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350336380 news.xs4all.nl 6979 [2001:888:2000:d::a6]:57631 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31344 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]])