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

Yiou Li liyiou at gmail.com
Wed Apr 13 01:50:56 CEST 2011


Hi Joe,

Thanks for the code! That works for me pretty well.

Best,
Leo


On Mon, Apr 11, 2011 at 3:04 PM, Joe Louderback <jglouderback at gmail.com> wrote:
> 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]])
> lowerTri(A)
> 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
>
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>


More information about the Baypiggies mailing list