简体   繁体   English

在Python中,将值-1分配给Mat文件中的数组会导致255

[英]In Python, assigning value -1 to an array from mat file results in 255

When I print the following array, all the values are 255 instead of -1. 当我打印以下数组时,所有值都是255而不是-1。 Any ideas? 有任何想法吗? I guess it has something to do with 8 bits, but I can't understand it. 我想这与8位有关,但我听不懂。 I want to assign the -1 value to all the values in the array. 我想将-1值分配给数组中的所有值。 I know there are workarounds like creating a new array with same size, I don't want that, I want to understand the problem. 我知道有一些变通方法,例如创建一个具有相同大小的新数组,我不想要那样,我想了解这个问题。

dados = scipy.io.loadmat('myDataset.mat')
ds_Y_Test = dados['Y']

for value in ds_Y_Test:
    value[0]=-1

-1 in two's complement binary is 11111111, which is 255 in unsigned binary, so that seems like a good guess! 二进制补码-1中的-1是11111111,无符号二进制中的255是255,所以这似乎是一个不错的猜测! It looks like you've loaded as the data as uint8 - you will have to convert the array (eg with .astype ). 看起来您已将数据作为uint8加载-您将不得不转换数组(例如,使用.astype )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM