Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 How to create meshgrid with non-integer stepsize of list elements? dazzabaijan Unladen Swallow Posts: 1 Threads: 1 Joined: Mar 2019 Reputation: 0 Likes received: 0 #1 Mar-10-2019, 11:23 AM (This post was last modified: Mar-10-2019, 11:23 AM by dazzabaijan. Edited 1 time in total. Edit Reason: wrong code tag format ) 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 ``````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] ```? ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 272 Likes received: 1264 #2 Mar-11-2019, 02:00 AM 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 - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post dict elements are sometimes treated as List and sometimes as String phython_mdr 4 234 Apr-01-2020, 12:47 PM Last Post: phython_mdr TypeError indexing a range of elements directly on the list JFerreira 2 219 Mar-30-2020, 04:22 PM Last Post: bowlofred Create a program that PING a list of IPs skaailet 7 274 Mar-26-2020, 10:46 PM Last Post: snippsat adding elements to a list that are more than a specific number Olavv 2 199 Mar-19-2020, 06:05 PM Last Post: Olavv Create a list of co-ordinates from Mouse press events Jagruthi 0 146 Mar-12-2020, 08:46 AM Last Post: Jagruthi How do i multiply elements in a list together Olavv 6 283 Mar-09-2020, 10:06 AM Last Post: Olavv Adding elements to a list by number Olavv 4 281 Mar-08-2020, 11:16 AM Last Post: ndc85430 List of n random elements medatib531 5 401 Mar-07-2020, 07:09 PM Last Post: jefsummers Trying to print corresponding elements in two different list Darthmarvolo 5 284 Mar-03-2020, 04:29 AM Last Post: perfringo multiplying elements in a list Olavv 3 368 Feb-27-2020, 04:55 PM Last Post: DeaD_EyE

Forum Jump:

Users browsing this thread: 1 Guest(s)