I would like to reduce a NumPy matrix using the vector u and the numpy.compress() method, first going across the rows and then columns. Now my code looks like this:
n = 4 #number of rows/columns
square_matrix = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
u = np.array([1,0,1,0])
v = []
for i in range(n):
v.append(np.compress(u,square_matrix[i]))
print(v)
I get the following output:
[array([1, 3]), array([5, 7]), array([ 9, 11]), array([13, 15])]
I have two questions:
- How can I now create a matrix from the output again.
- How could I repeat the same process for the columns. (My initial idea was to use a transpose of
u, something like this:
for j in range((len(v_matrix[0])-1)):
w.append(np.compress(u.transpose(),v_matrix[:][j]))
uis 1d, so transpose doesn't change anything. Looks a bit like you read just part of thenp.compressdocs, ignoring or no understanding theaxispart.