Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Efficient python solution to subset a matrix based on row conditions

$
0
0

I'm using python 3.9/numpy 1.22.

Suppose I have a 3x3 matrix:

x = np.array([[10,40,0],[0,40,90],[10,0,90]]).

All elements are integers > 0.Every row has exactly 2 non-zero integers.

I would like to produce the 3x2 matrix y such that

y = np.array([[10,40],[40,90],[10,90]])

I feel close when using numpy.apply_along_axis(), numpy.squeeze(), and/or numpy.where() but I'm missing something.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>