Upper Triangular Matrix From Array --Python
1
0
Entering edit mode
10.1 years ago
st.ph.n ★ 2.7k

I have a list of 49 values. I've created an array of this list, to print in a matrix, with sequence identifiers from a fasta file. Here is the code:

print my_plist
print len(my_seq_labels)

row_col = len(my_seq_labels)   

size =7
myarray = [my_plist[w:w+size+1] for w in range(0, len(my_plist), size)]

print "\t\t",
for j in range(row_col):
    print my_seq_labels[j],"\t",
print
for i in range(row_col):
    print my_seq_labels[i],"\t",
    for j in range(row_col):
        print "%.4f" % myarray[j][i],"\t\t",
    print

This is the output:

[0.0, 0.006535947712418301, 0.00980392156862745, 0.08496732026143791, 0.08169934640522876, 0.08169934640522876, 0.13398692810457516, 0.006535947712418301, 0.0, 0.00980392156862745, 0.08496732026143791, 0.08169934640522876, 0.08169934640522876, 0.13398692810457516, 0.00980392156862745, 0.00980392156862745, 0.0, 0.0784313725490196, 0.07516339869281045, 0.07516339869281045, 0.12745098039215685, 0.08496732026143791, 0.08496732026143791, 0.0784313725490196, 0.0, 0.0032679738562091504, 0.0032679738562091504, 0.13398692810457516, 0.08169934640522876, 0.08169934640522876, 0.07516339869281045, 0.0032679738562091504, 0.0, 0.006535947712418301, 0.13071895424836602, 0.08169934640522876, 0.08169934640522876, 0.07516339869281045, 0.0032679738562091504, 0.006535947712418301, 0.0, 0.13071895424836602, 0.13398692810457516, 0.13398692810457516, 0.12745098039215685, 0.13398692810457516, 0.13071895424836602, 0.13071895424836602, 0.0]
7
        410488935     410488927     410488931     410488939     410488937     410488923     410488933     
410488935     0.0000         0.0065         0.0098         0.0850         0.0817         0.0817         0.1340         
410488927     0.0065         0.0000         0.0098         0.0850         0.0817         0.0817         0.1340         
410488931     0.0098         0.0098         0.0000         0.0784         0.0752         0.0752         0.1275         
410488939     0.0850         0.0850         0.0784         0.0000         0.0033         0.0033         0.1340         
410488937     0.0817         0.0817         0.0752         0.0033         0.0000         0.0065         0.1307         
410488923     0.0817         0.0817         0.0752         0.0033         0.0065         0.0000         0.1307         
410488933     0.1340         0.1340         0.1275         0.1340         0.1307         0.1307         0.0000

How can this code be modified in the for loop where the array prints, to print in an upper triangular matrix? (as well as lower)

Any help is appreciated.

python • 15k views
ADD COMMENT
0
Entering edit mode

May be this can help: numpy.triu (triangle-upper) and numpy.tril (triangle-lower).

ADD REPLY
2
Entering edit mode
10.1 years ago
cnluzon ▴ 30

So, as far as I have understood it, you want to skip the printing of the upper/lower triangle of a matrix because it is a symmetric one, right?

This can be controlled by the inner part of the loop (the part where you go through the columns with the j index).

So, if you want to print only the lower part, something like this should work:

for j in range(row_col):
   print my_seq_labels[j],"\t",
print
for i in range(row_col):
    print my_seq_labels[i],"\t",
    for j in range(i+1):
        print "%.4f" % myarray[i][j],"\t\t",
    print

The result of this is:

        410488935     410488927     410488931     410488939     410488937     410488923     410488933     
410488935     0.0000         
410488927     0.0065         0.0000         
410488931     0.0098         0.0098         0.0000         
410488939     0.0850         0.0850         0.0784         0.0000         
410488937     0.0817         0.0817         0.0752         0.0033         0.0000         
410488923     0.0817         0.0817         0.0752         0.0033         0.0065         0.0000         
410488933     0.1340         0.1340         0.1275         0.1340         0.1307         0.1307         0.0000

If you want to skip the diagonal you can put just "for j in range(i)" instead of i+1.

For the upper matrix you have to justify it with '\t" characters:

for j in range(row_col):
    print my_seq_labels[j],"\t",
print
for i in range(row_col):
    print my_seq_labels[i],"\t",
    for j in range(row_col):
        if j < i:
            print "\t\t",
        else:
            print "%.4f" % myarray[i][j],"\t\t",
    print

The output of this would be the upper side of the matrix. Again, if you want to skip the diagonal, the "if i < j" could be i<=j or i < j+1.

I hope this helped.

ADD COMMENT

Login before adding your answer.

Traffic: 2735 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6