473,503 Members | 756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Duplicate entries in a matrix

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

Jan 18 '06 #1
5 2197
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

Jan 19 '06 #2
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

Jan 19 '06 #3
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


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
ro*********@gmail.com

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

Jan 19 '06 #4
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!

Jan 19 '06 #5
Chris Lasher wrote:
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
ro*********@gmail.com

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

Jan 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3086
by: Gary Lundquest | last post by:
I have an application with MS Access 2000 as the front end and MySQL as the back end. All was well until I upgraded the MySQL (Linux) server. The Problem: I insert data into a cumulative table....
1
838
by: Gary Lundquest | last post by:
It appears to me that MySQL version 4 returns an error messge when doing an Insert that results in duplicate entries. Version 3 did NOT return an error - it dropped the duplicate entries and ran...
1
14004
by: marx | last post by:
I have a bit of a problem and any help would be much appreciated. Problem: I have two dropdown list boxes with same data(all data driven). These are used for two separate entries. For every...
3
6909
by: andreas.maurer1971 | last post by:
Hi all, since a few years I use the following statement to find duplicate entries in a table: SELECT t1.id, t2.id,... FROM table AS t1 INNER JOIN table AS t2 ON t1.field = t2.field WHERE...
5
3975
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
6
4529
by: teser3 | last post by:
I have my PHP inserting into Oracle 9i. But how do I prevent duplicate record entries? I only have 3 fields in the insert in the action page: CODE <?php $c=OCILogon("scott", "tiger",...
1
1852
by: Phox | last post by:
I'm learning Python, and as a small exercise I made for myself I'm writing a program to find the determinate of a 2x2 matrix. I'm running Python 2.5 on Windows Vista, here's my code. #for loop...
4
2866
by: ramdil | last post by:
Hi All I have table and it have around 90000 records.Its primary key is autonumber field and it has also have date column and name, then some other columns Now i have problem with the table,as my...
7
2893
by: php_mysql_beginer911 | last post by:
Hi .. i am trying to update a table where if field contents any duplictaed entries than one of the field should be updated with random number which is unique so i can make all entries unique i...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.