[Baypiggies] Question about collecting lower/upper triangle elements from a python array
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
return np.concatenate([ x[i][:i+1] for i in xrange(x.shape) ])
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!
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Baypiggies