Duplicate entries in a matrix
Gerard Flanagan
grflanagan at yahoo.co.uk
Wed Jan 18 19:05:01 EST 2006
Chris Lasher wrote:
> Hello Pythonistas!
> I'm looking for a way to duplicate entries in a symmetrical matrix
> that's composed of genetic distances. For example, suppose I have a
> matrix like the following:
>
> A B C
> A 0.000000 0.500000 1.000000
> B 0.500000 0.000000 0.500000
> C 1.000000 0.500000 0.000000
>
> Say I want to duplicate entry B; the resulting matrix would look like:
>
> A B B C
> A 0.000000 0.500000 0.500000 1.000000
> B 0.500000 0.000000 0.000000 0.500000
> B 0.500000 0.000000 0.000000 0.500000
> C 1.000000 0.500000 0.500000 0.000000
>
> The cases I'd like to do this for are more complicated, naturally,
> where I want to duplicate different entries different numbers of times.
> I'm aware of Numeric, Numarray, and NumPy, though I've not used any of
> them, but I am not sure if any of them possess a simple means to
> duplicate entries in a matrix. I started writing code on my own but I
> can see it is becoming exponentially more complex, and before I proceed
> any further, I want to make sure I'm not reinventing any wheels. If
> anyone here has any advice on how to manipulate matrices in this
> manner, I'd _greatly_ appreciate it!
>
> Thanks in advance,
> Chris
Chris
Very rusty at this sort of thing, but if you premultiply the first
matrix by:
1 0 0
0 1 0
0 1 0
0 0 1
and postmultiply the result by:
1 0 0 0
0 1 1 0
0 0 0 1
I think you get what you want. (If that helps).
Gerard
More information about the Python-list
mailing list