Python Forum
extract lower/upper antitriangular matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extract lower/upper antitriangular matrix
#1
In numpy you can use np.tril(x) to extract the lower triangular matrix of an array x. However, I am interested in extracting the lower (upper) antitriangular matrix, that is, with zeros above (below) the antidiagonal. Is there a way to do this in python?
Reply
#2
Look at the numpy and scipy linear algebra libraries.
Reply
#3
You could perhaps compose np.tril() and np.triu() with np.flip()
>>> import numpy as np
>>> a = np.array(range(1, 17)).reshape((4,4))
>>> a
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])
>>> np.flip(a, 0)
array([[13, 14, 15, 16],
       [ 9, 10, 11, 12],
       [ 5,  6,  7,  8],
       [ 1,  2,  3,  4]])
>>> np.flip(np.tril(np.flip(a, 0)), 0)
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  0],
       [ 9, 10,  0,  0],
       [13,  0,  0,  0]])
>>> np.flip(np.triu(np.flip(a, 0)), 0)
array([[ 0,  0,  0,  4],
       [ 0,  0,  7,  8],
       [ 0, 10, 11, 12],
       [13, 14, 15, 16]])
>>> 
schniefen likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Higher or lower game SEWII 17 3,969 May-28-2023, 11:59 AM
Last Post: jefsummers
  define a diagonal matrix from a matrix amalalaoui 1 2,345 May-15-2019, 01:12 PM
Last Post: ichabod801
  HELP - Returning self numbers lower or equal to my argument Kokuzuma 4 2,735 Nov-01-2018, 06:35 PM
Last Post: Kokuzuma
  how to use the upper() King 5 3,998 Sep-26-2017, 10:51 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020