I have a set of data that is not smooth. Hence I'm trying to interpolate it with the generated mesh.
import numpy as np
data=np.loadtxt('test.txt')
x=data[:,0] #len(x) = 730
y=data[:,1]
Nx= len(x)
Ny=len(y)
del_x= 0.5
xn = np.linspace(0,Nx,2000)
yn = np.linspace(0,Ny,2000)
#loop
for i in range(0,Nx-1):
if x[i] > xn[i] and x[i] < xn[i+1]:
new_x= (i + (xn[i] - x[i])/(xn[i]-xn[i+1]))*del_x
print new_x
I would like to perform the loop that basically does the operation ie : if my original data x[i] is in between the two grid points xn[i],xn[i+1], then compute new_x.
But I get the following error
Traceback (most recent call last):
File "new.py", line 27, in <module>
if x[i] > xn[i] and x[i] < xn[i+1]:
IndexError: index out of bound
Can someone help me out ?
If len(x) = 730
than its too short for the loop. Your loop iterates 1000
times, i
would be at some point bigger than 730
and there are no values like x[730]
, x[731]
etc. You will get an out of bound error because you are accessing not existing values in an array.
您可以这样更改for循环:
for i in range(0, len(x)):
0
. [start, stop[
, meaning that the last element is not included. Just see what happens if you try:
a = [1, 2, 3]
print("a has %d elements:" %(len(a)))
for i in range(len(a)):
print("a[%d] = %d" %(i, a[i]))
The output is:
a has 3 elements:
a[0] = 1
a[1] = 2
a[2] = 3
If you try to access a[3]
you will see:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
Numpy "linspace" takes the following input: numpy.linspace(start, stop, N)
, resulting in a closed range [start, stop]
(notice it includes the "stop") with N
elements.
You want the number of elements, or len(xn)
to be 730, and I am guessing you want the "stop" value to be max(x)
:
xn = numpy.linspace(0, max(x), len(x))
for i in range(len(x)):
...
Tip: If you are uncertain about the signature of a function you can always ask for
help
, try:help(numpy.linspace)
numpy linspace official documentation
xn = np.linspace(0,Nx,2000)
in here 0
is start value, Nx
is end value and 2000
is the number of values to be generated.
returned object of linspace is something like :
[0, 0.5, 1, ... , ] #2000 elements
ie a row containing 2000 elements
x=data[:,0]
x is something like :
[[1],[2],[3],[4], .... ,[]] #column vector with number of elements in text data
now row size of x can not be greater than 1
ie index has to be less than 1
or equal to 0
and you can't compare these two arrays
[ ... ]
and [[], [], [], [], ... ]
are incomparable (you can compare the first element though.)
you could use this : reshape
refer to this answer : What does -1 mean in numpy reshape?
row_x = x.reshape(-1)
will give you a row vector.
and you can convert to column format by:
col_x = row_x.reshape(-1,1)
I would appreciate edits as I am currently at work and thus wrote the answer in hurry.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.