Reputation: 194
I have a list that contains 1024 elements. I want to check the maximum value of the list between an interval that i determined.
for example X is my list that is in numpy array form. Then;
if np.amax(X[0:31]) > 200:
print("1")
elif np.amax(X[0:31]) < 200:
print("1a")
if np.amax(X[32:63] > 200:
print("2")
elif np.amax(X[32:63] < 200:
print("2a")
and if - elif statements goes untill X[992:1023]. That means there are 16 intervals and 16 if-elif statements. Is there more efficient way to do that?
edit:Thanks for the answer. But the problem is a bit different. Let me try to explain it with an example. Each result of these intervals lights up an LED. There are 16 intervals and 16 different LEDs. Further more maximum value bounds have 3 layer. I did not mention about it. But such as (max<200), (200>max<500), (max > 500). It is a bit complicated.
Upvotes: 0
Views: 2131
Reputation: 1327
You could reshape your matrix to a two dimensional matrix based on your interval and use the max function on a specific axis.
interval = 32
Xnew = X.reshape(interval, len(X)/interval)
print np.max(Xnew, axis=1) # This is an array of the max values of each consecutive interval range in X
Upvotes: 0
Reputation: 73460
Well, for such regular intervals, a standard for-loop should do. For starters, this will work:
for x in xrange(0, 1024, 32): # 0, 32, 64, ... , 992
m = np.amax(X[x:x+32])
if m > 200:
print(str(x/32 + 1)) # 1, 2, 3, ... , 32 (not 16)
elif m < 200:
print(str(x/32 + 1) + 'a') # 1a, 2a, 3a, ...
I assumed that you want to include index 31
in the first interval. Your code will omit indexes 31
, 63
, etc.
Upvotes: 1