Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31347

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

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!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <newsboost@gmail.com>
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; 'mrab': 0.05; 'much!': 0.05; 'newbie': 0.05; 'python': 0.09; '[0,': 0.09; 'str):': 0.09; 'variables,': 0.09; '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; 'ok...': 0.16; 'problem!': 0.16; 'subject:array': 0.16; "test')": 0.16; 'uh,': 0.16; 'wrote:': 0.17; 'variables': 0.17; 'obviously': 0.18; '>>>': 0.18; '(or': 0.18; 'thanks.': 0.21; 'received:mail-bk0-f46.google.com': 0.22; '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; 'appreciated.': 0.26; '(most': 0.27; 'received:209.85.214.46': 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; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'data,': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'test': 0.36; 'should': 0.36; 'thank': 0.36; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'skip:u 10': 0.60; 'places': 0.61; 'subject:...': 0.63; 'skip:n 10': 0.63; 'great': 0.64
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=gHl2HtA5DSsYOdypIP8vg99jJ92S4MEahCrvOOrHN7w=; b=Yqn0pGYkFzRKMHw0tI5O/2U/AwG/X/0EOoRqO629smCwUSWCRHFeJ2mRlP1O2YeAIf STVEVbevz7goq97DD6IBQURYKOwCcuaAP3xHteCXRCRnuHXW+B3I53uO1sslqtqPunfK DUeG/15S/OV0n3mEEOkfcyYOVDbQEV0FcMheuvaZyhP7m4a60xK/UEzj+nYrN51+pnnk Ja950RVl+5T/5Uk0X4kOM49pWDEsKXK77cELMWm2tpsdliyTsAZAsS9Pf/YwWPVJ7a7J sjXbdlhF/D0FtlEhHuYNyjMc5mDXo8AOu20Z0fna8Yfxl1utas45k5gxEehAS11n2cze aXsg==
Date Tue, 16 Oct 2012 00:42:34 +0200
From someone <newsboost@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: numpy - 2D matrix/array - initialization like in Matlab...
References <k5hu2e$h5i$1@dont-email.me> <mailman.2241.1350336380.27098.python-list@python.org>
In-Reply-To <mailman.2241.1350336380.27098.python-list@python.org>
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
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2245.1350340959.27098.python-list@python.org> (permalink)
Lines 94
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350340959 news.xs4all.nl 6947 [2001:888:2000:d::a6]:47235
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31347

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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