Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to create meshgrid with non-integer stepsize of list elements?
I have 2 lists of x and y coordinate that are independently generated, with a/h amount of points between 0 and a.
    x = np.linspace(0, a, a/h)
    y = np.linspace(0, d, d/h)
when a/h is such that 0 increases to a in steps of integers i.e. [0,1,2,..,a]. It's nice because then the number of elements within the list can be used as indices. And as a result I can usually create a meshgrid such that a third list V1 can be associated with it.
    X, Y = plt.meshgrid(x, y)
    def potential(V1):
        return V1[X, Y]
where potential(V1) is now V1 corresponding to the meshgrid [x, y]. However I'm doing an assignment where I'm required to investigate how step-sizes affect my problem. As a result if I was to have a step-size of non-integers from 0 to a i.e. [0, 0.5, 1,...,a] Now I can't do what I did above since the indices are now non-integers. Raising the error
IndexError: arrays used as indices must be of integer (or boolean) type
How can I fix this so that I don't rely on the value of the element itself as the index of the elements, so that if there was a step-size of 0.25 between 0 to a for a list X say i.e.
    X = [0, 0.25, 0.75. 1.0] or x = np.linspace(0,1,4)
such that I can have
    x[0] = 0 corresponds to V[0]
    x[1] = 0.25 corresponds to V[1]
    x[2] = 0.75 corresponds to V[2]
    x[3] = 1 corresponds to V[3]
Couldn't you kick it up a notch, so that all the values are integers? For example, x = np.linspace(0, a * h, h). It would require dividing by h elsewhere in your code, but it is doable.

Or you could do what your last example implies: x = [0, 0.25, 0.5, 0.75, 1.0], and then use the integers 0 through 4 for your indexes. You then translate the indexes using x.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Create a dictionary from a list klllmmm 3 149 Oct-06-2019, 05:50 PM
Last Post: Gribouillis
  addition for elements in lists of list ridgerunnersjw 3 184 Sep-15-2019, 07:11 AM
Last Post: perfringo
  memory error using permutation list of 11 elements kikidog 1 130 Sep-10-2019, 08:22 PM
Last Post: ichabod801
  Count elements in a list only if they are following each other and are similar law 10 451 Aug-02-2019, 10:54 PM
Last Post: law
  How to create a list with all values being zero 357mag 4 247 Jul-14-2019, 03:43 PM
Last Post: ThomasL
  Changing elements of a list to match another list rockproper 2 204 Jul-11-2019, 02:13 PM
Last Post: ichabod801
  Create new list from another list based on condition khalidreemy 1 284 Jun-11-2019, 10:21 AM
Last Post: buran
  swap elements in list hshivaraj 3 1,623 Apr-22-2019, 09:23 AM
Last Post: Yoriz
  flattening a list with some elements being lists Skaperen 17 1,377 Apr-09-2019, 07:08 AM
Last Post: perfringo
  Detecting String Elements in a List within Given Message Redicebergz 6 542 Mar-19-2019, 03:12 PM
Last Post: buran

Forum Jump:

Users browsing this thread: 1 Guest(s)