1

How can I loop over a matrix and get the number of columns in each row? If I have a matrix, and some elements are NaN (empty) in the matrix, e.g.: [[4,2,9,4],[3,4,8,6],[5,NaN,7,7],[Nan,8,Nan,Nan]], how can I compute the row-wise length?

I have tried:

len(matrix) # number of rows
=len(matrix[0]) # number of columns

But that gives me the total number.

So I want to get a vector saying the number of columns in each row: [4,4,3,1] e.g.

My idea is to make a loop like this:

for i in matrix:

And then a loop where it searches. But I'm not sure how to do this

EDIT: I tried @wavy's method and it worked. Can I do like this:

# empty list
Final=[]

for i in range(matrix):
    columns=np.isnan(matrix).sum(axis=1)
    result=-columns+matrix.shape[1]
    if result==1:
        Final.append(matrix[i])
        
        
    print(Final)

I also need to put other conditions, when result==2, and when result>2
3
  • Do you want to use this on large matrices? Commented Jun 20, 2020 at 10:52
  • No I'm trying now with a 3 (columns)x8 (rows) matrix Commented Jun 20, 2020 at 11:14
  • In a numpy array all columns have the same length. np.nan doesn't count as "empty". Sounds instead like you want to count the number of non-nan values. Which is fine, but the description should be clearer. Commented Jun 20, 2020 at 15:39

2 Answers 2

1

This might be faster than David Wierichs suggestion:

import numpy as np 

x = np.array([[4, 2, 9, 4], [3, 4, 8, 6], [5, np.nan, 7, 7], [np.nan, 8, np.nan, np.nan]])
y = np.isnan(x).sum(axis=1)
result = -y + x.shape[1]
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! Now that I have this, can I then somehow get it to say "if length of that row is 1 (so only where there is 1 number) then the number in the new vector should be that number"?
Something like this: for i in range(matrix): columns=np.isnan(matrix).sum(axis=1) result=-columns+matrix.shape[1] if result==1: # then here it should keep that number in the vector where result is 1 print(result)
After having done what I posted there you can try this: x[np.isnan(x)] = 0 for i in range(len(result)): if result[i] == 1: result[i] = np.max(x[i, :]). There's likely a faster way than looping over the result array, but I cannot immediately think of it.
Is it possible somehow I can ask you some more questions in any way?
I'm online for a bit longer, we can chat here I think: chat.stackoverflow.com/rooms/216320/discuss-numpy-question
|
0

You could loop over the rows and for each row use the (negated) numpy.isnan method:

lengths = [np.sum(~np.isnan(row)) for row in matrix]

As this builds up a boolean array in the np.isnan, there might be faster approaches.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.