Duplicate entries in a matrix

C

Chris Lasher

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
 
G

Gerard Flanagan

Chris said:
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
 
C

Chris Lasher

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]])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]])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]])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
 
R

Robert Kern

Chris said:
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

In [1]: from numpy import *

In [2]: A = array([[0.0, 0.5, 1.0],
...: [0.5, 0.0, 0.5],
...: [1.0, 0.5, 0.0]])

In [3]: B = repeat(A, [1,2,1])

In [4]: B
Out[4]:
array([[ 0. , 0.5, 1. ],
[ 0.5, 0. , 0.5],
[ 0.5, 0. , 0.5],
[ 1. , 0.5, 0. ]])

In [5]: C = repeat(B, [1,2,1], axis=-1)

In [6]: C
Out[6]:
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. ]])

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
C

Chris Lasher

Now that's definitely what I'm looking for! Thanks!

By the way, was this line

In [5]: C = repeat(B, [1,2,1], axis=-1)

supposed to have a positive 1 value for axis? It works either way, I
see. Is it like a lookup, where an index of -1 returns the last value?
If that were true, I supposed the evaluation would be 1, and thus gives
the same result.

Thanks again, very much. You guys are super-helpful!
 
R

Robert Kern

Chris said:
Now that's definitely what I'm looking for! Thanks!

By the way, was this line

In [5]: C = repeat(B, [1,2,1], axis=-1)

supposed to have a positive 1 value for axis? It works either way, I
see. Is it like a lookup, where an index of -1 returns the last value?
If that were true, I supposed the evaluation would be 1, and thus gives
the same result.

That is correct.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top