Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

我写了一个循环,里面有个if判断,根据判断结果给矩阵赋值data_old[i,j] = 266,结果每次赋值都矩阵那个位置的值不是266,而是10,这是为什么?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
430 views
Welcome To Ask or Share your Answers For Others

1 Answer

估计你定义的数据类型的原因,如果是 uint8,则会溢出:

>>> import numpy as np
>>> a = np.array([1, 2, 3], dtype = np.uint8) 
>>> a
array([1, 2, 3], dtype=uint8)
>>> a[0]=266
>>> a
array([10,  2,  3], dtype=uint8)
>>> a.itemsize
1

uint8的取值范围是0~255, 266-256=10.
itemsize属性会返回类型字节数。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...