Python Forum
Getting largest indices of array less than or equal to an array of numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting largest indices of array less than or equal to an array of numbers
#1
Consider the following code:

import numpy as np
y_i=np.linspace(0,1,11) #0,0.1,0.2,...,1
y=np.random.uniform(0,1) #random point in [0,1)
i=np.argmax(y_i>=y) #returns the index of the smallest number in y_i greater than or equal to y
If y is an array of random points, and one would like to attain all the indices in y_i for each of these points, how would one go about doing that using np.argmax or any other one-liner if it exists?

Current solution:
d=2
y=np.random.uniform(0,1,d)
i=np.zeros(d)
for l in range(0,d):
  i[l]=np.argmax(y_i>=y[l])
Reply
#2
Your solution is quite slow because you are using pure Python loops. For this problem
complete vectorized solution exists:

np.argmax(np.repeat(y_i[:, np.newaxis], len(y), axis=1) >= y, axis=0)
It is expected that y is a numpy array of shape (len(y), ).
schniefen likes this post
Reply
#3
Suppose y_i is a matrix such that
y_i=np.linspace(np.zeros(d),np.ones(d),m+1)  
That is, each column is an array given by np.linspace(0,1,m+1) and the entries between 0 and 1, i.e. the first and last row, might be assigned different values in a later statement. Then for y being an array of random points, I'd like to know, for each column of the matrix y_i the index of the number greater than or equal to a random point in y. In other words, I'd like to get a matrix of indices of shape (len(y),d).
Reply
#4
Current solution:
y_i=np.linspace(np.zeros(d),np.ones(d),m+1) 
y=np.random.uniform(0,1,d)                      
ind=np.zeros([len(y),d]) 
for k in range(d):
  k_i=y_i[:,k]
  i=np.argmax(np.repeat(k_i[:, np.newaxis], len(y), axis=1) >= y, axis=0)
  ind[:,k]=i
Reply
#5
I realize that what I want is not quite what I have said, so I'm restating it:

Suppose
y_i=np.linspace(np.zeros(d),np.ones(d),m+1)
and the values between 0 and 1 (the first and last row) are changed in a later statement of the code. Then, for

y=np.random.uniform(0,1,d)
I'd like to find the index of the number in the i'th column of y_i greater than or equal to the i'th entry of y.
Reply
#6
Current solution:

y_i=np.linspace(np.zeros(d),np.ones(d),m+1)
y=np.random.uniform(0,1,d)                      
ind=np.zeros(d) 
for k in range(d):
   k_i=y_i[:,k]
   i=np.argmax(k_i >= y[k])
   ind[k]=i
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie here. Create an array from file data? Rayj00 2 1,248 Jan-13-2023, 01:35 PM
Last Post: perfringo
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,428 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  random interger array question rpang 3 1,843 Nov-05-2022, 12:31 PM
Last Post: deanhystad
  2-D array imrobinotmar 0 1,191 Jul-11-2022, 01:11 PM
Last Post: imrobinotmar
  Array Indices? DaveG 1 1,274 Mar-20-2022, 05:57 AM
Last Post: deanhystad
  an array to count votes thechungusmaximus 2 1,514 Mar-09-2022, 03:44 PM
Last Post: DeaD_EyE
  How to multiply tuple values in array? EngiPorem 3 3,030 Aug-27-2021, 03:26 AM
Last Post: naughtyCat
  Convert list of numbers to string of numbers kam_uk 5 3,019 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  create new variable that sums the array prasanthbab1234 3 2,320 Sep-26-2020, 02:03 PM
Last Post: jefsummers
  Linked List - Ordering by largest population with country name. PaleHorse 2 2,950 Jun-16-2020, 09:04 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020