[Baypiggies] Question about collecting lower/upper triangle elements from a python array

Joe Louderback jglouderback at gmail.com
Tue Apr 12 00:04:01 CEST 2011

The following gives a 1-d vector reading the lower triangular values by row.
 There may be more efficient ways to do this, especially if you are
guaranteed there aren't any lower triangular elements equal to 0.

import numpy as np
def lowerTri(x):
    return np.concatenate([ x[i][:i+1] for i in xrange(x.shape[0]) ])

A = np.array([[2,  4,  6],[8, 10, 12], [14, 16, 18]])
array([ 2, 8, 10, 14, 16, 18 ])

-- Joe L.

On Mon, Apr 11, 2011 at 2:24 PM, Yiou Li <liyiou at gmail.com> wrote:

> Dear all,
> I have a N x N array and want to obtain the lower triangle half of the
> array elements and arrange them into a 1-dimensional data vector.
> I googled a bit and find the numpy.tril() function but it just zero
> out the upper triangle elements so it doesn't work for me. I also
> tried y = x(tril(x)!=0) but it gives me error.
> You advise is very much appreciated!
> Leo
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20110411/715a5518/attachment.html>

More information about the Baypiggies mailing list