Duplicate entries in a matrix

Chris Lasher chris.lasher at gmail.com
Thu Jan 19 12:52:15 EST 2006


Hey Gerard,

Thanks for the suggestion! It took me a while to figure out how to get
this to work. Two things were important: I needed to use the
matrixmultiply() function, and the order of the two matrices being
multiplied is critcial. Here's how I got the example to work.

>>> from Numeric import *
>>> array1 = array([[0.000000, 0.500000, 1.000000],
...     [0.500000, 0.000000, 0.500000],
...     [1.000000, 0.500000, 0.000000]])
>>> array1
array([[ 0. ,  0.5,  1. ],
       [ 0.5,  0. ,  0.5],
       [ 1. ,  0.5,  0. ]])
>>> premul = array([[1, 0, 0], [0, 1, 0], [0, 1, 0], [0, 0, 1]])
>>> premul
array([[1, 0, 0],
       [0, 1, 0],
       [0, 1, 0],
       [0, 0, 1]])
>>> res1 = matrixmultiply(premul, array1)
>>> res1
array([[ 0. ,  0.5,  1. ],
       [ 0.5,  0. ,  0.5],
       [ 0.5,  0. ,  0.5],
       [ 1. ,  0.5,  0. ]])
>>> postmul = array([[1, 0, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1]])
>>> postmul
array([[1, 0, 0, 0],
       [0, 1, 1, 0],
       [0, 0, 0, 1]])
>>> array2 = matrixmultiply(res1, postmul)
>>> array2
array([[ 0. ,  0.5,  0.5,  1. ],
       [ 0.5,  0. ,  0. ,  0.5],
       [ 0.5,  0. ,  0. ,  0.5],
       [ 1. ,  0.5,  0.5,  0. ]])

Now I have to figure out how to expand this concept to more complex
cases, and how to generate appropriate pre- and post-multiply
matrices... Hmm...

Thanks so much for getting me started,
Chris




More information about the Python-list mailing list