0

I'm new to NumPy, a problem blocks me.. --- I want to change a ndarray's value:

Here is the debug info.

(Pdb) Nodes[0,0]['f'] = np.array([i/9.0 for i in  range(9)])
(Pdb) print Nodes[0,0]['f']
[  0.00000000e+00   0.00000000e+00   5.67382835e+10   4.58280650e-41
   1.00030523e-36   0.00000000e+00   1.00030523e-36   0.00000000e+00
   2.28153811e-40]
(Pdb)

Why doesn't the value of Node[0,0]['f'] change?

1
  • 5
    More information needed: what you've posted doesn't show that the value of Nodes[0,0]['f'] doesn't change, only that it didn't change into what you wanted it to. What are the values and types of Nodes[0,0] and Nodes[0,0]['f']? Can you make a short example which shows the problem that we can cut and paste? Commented Mar 11, 2012 at 15:50

1 Answer 1

1

Try using Nodes['f'][0,0] = numpy.array([i/9.0 for i in range(9)]) instead:

import numpy
Nodes = numpy.ndarray(shape=(1,1), dtype=[('f', (float, 9))])
print Nodes[0,0]['f']
# [  0.00000000e+000   2.10042365e-316   2.44222340e-316   6.90749588e-310
#    2.10041417e-316   4.22653002e-317   2.76341350e-316   6.90749588e-310
#    3.95252517e-322]
Nodes[0,0]['f'] = numpy.array([i/9.0 for i in range(9)])
print Nodes[0,0]['f']
# [  0.00000000e+000   2.10042365e-316   2.44222340e-316   6.90749588e-310
#    2.10041417e-316   4.22653002e-317   2.76341350e-316   6.90749588e-310
#    3.95252517e-322]
Nodes['f'][0,0] = numpy.array([i/9.0 for i in range(9)])
print Nodes[0,0]['f']
# [ 0.          0.11111111  0.22222222  0.33333333  0.44444444  0.55555556
#   0.66666667  0.77777778  0.88888889]

I'm not sure why there's a difference, but it is probably related to this question.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.