[Numpy-discussion] saving the lower half of matrix
Matthew Brett
matthew.brett@gmail....
Mon Jun 17 06:19:55 CDT 2013
Hi,
On Mon, Jun 17, 2013 at 12:07 PM, Bala subramanian
<bala.biophysics@gmail.com> wrote:
> Friends,
> I have to save only the lower half of a symmetric matrix to a file. I used
> numpy.tril to extract the lower half. However when i use 'numpy.savetxt',
> numpy saves the whole matrix (with upper half values replaced as zeros)
> rather than only the lower half. Any better idea to achieve this.
>
> as an example
>>>>import numpy as np
>>>> d=np.arange(1,26,1)
>>>> d=d.reshape(5,5)
>>>> np.tril(d)
> array([[ 1, 0, 0, 0, 0],
> [ 6, 7, 0, 0, 0],
> [11, 12, 13, 0, 0],
> [16, 17, 18, 19, 0],
> [21, 22, 23, 24, 25]])
>
>>>> np.savetxt( 'test', np.tril(d))
> output test also contains the zeros, what i want is only the lower half like
> below.
> 1
> 6 7
> 11 12 13
> - - - - etc
Maybe you want something like:
e = d[np.tril_indices(5)]
?
Best,
Matthew
More information about the NumPy-Discussion
mailing list