remove zero lines 2-D numpy array

multidimensional-array, numpy, python

Solution

Use `np.all` with an `axis` argument:

>>> r[np.all(r == 0, axis=1)]
array([[ 0.,  0.,  0.]])
>>> r[~np.all(r == 0, axis=1)]
array([[-1.41421356, -0.70710678, -0.70710678],
       [ 0.        , -1.22474487, -1.22474487]])

Problem

I run a `qr factorization` in `numpy` which returns a list of `ndarrays`, namely `Q`and `R`: ``` >>> [q,r] = np.linalg.qr(np.array([1,0,0,0,1,1,1,1,1]).reshape(3,3)) ``` `R` is a two-dimensional array, having pivoted zero-lines at the bottom (even proved for all examples in my test set): ``` >>> print r [[ 1.41421356 0.70710678 0.70710678] [ 0. 1.22474487 1.22474487] [ 0. 0. 0. ]] ``` . Now, I want to divide `R` in two matrices `R_~`: ``` [[ 1.41421356 0.70710678 0.70710678] [ 0. 1.22474487 1.22474487]] ``` and `R_0`: ``` [[ 0. 0. 0. ]] ``` (extracting all zero-lines). It seems to be close to this solution: deleting rows in numpy array. EDIT: Even more interesting: `np.linalg.qr()` returns a `n x n`-matrix. Not, what I would have expected: ``` A := n x m Q := n x m R := n x m ```

Original source

Related problems