i have this matrix called SP
:
[ 0. 134.825 280.45 280.45 280.45 280.45 280.45 280.45
280.45 280.45 280.45 145.625 0. 134.825 280.45 280.45
280.45 280.45 280.45 280.45 280.45 280.45 280.45 145.625
0. 134.825 134.825 134.825 134.825 134.825 235.45 235.45
235.45 235.45 235.45 100.625]
Now I want to create a new matrix called SP1
and loop through it. I'm pretty new in python, but I know that python requires to create the empty matrix outside the loop, and then sobstitute the values created.
I used this code:
SP1 = np.zeros((len(SP),))
for d in range(len(SP)):
if d ==0:
SP1[d,] = SP[d,]
else:
if SP[d-1,] < SP[d,]:
SP1[d-1,] = 0
SP1[d,] = SP[d,]
else:
SP1[d,] = SP[d,]
break
break
break
print(SP1)
However, although any errors appear, I get all zeros, like the matrix SP1 that I created. Anybody can guide me to find the problem?
This is my expected output:
[0 0 280.45 280.45 280.45 280.45 280.45 280.45 280.45
280.45 280.45 145.62 0 0 280.45 280.45 280.45 280.45 280.45
280.45 280.45 280.45 280.45 145.62 0 134.82 134.82 134.82
134.82 0 235.45 235.45 235.45 235.45 235.45 100.62]
Really thanks in advance.
Your issue is how you are indexing SP and SP1. You should be seeing an error like TypeError: list indices must be integers or slices, not tuple
. Also you don't need the breaks. Try this:
for d in range(len(SP)):
if d == 0:
SP1[d] = SP[d]
else:
if SP[d - 1] < SP[d]:
SP1[d - 1] = 0
SP1[d] = SP[d]
else:
SP1[d] = SP[d]
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.