小编ayu*_*pta的帖子

零填充 3d Numpy 数组

我有一个形状为 (103, 37, 13) 的 3d numpy 数组(数据)。我想通过在每个轴的两个方向上几乎相等地填充零来将此 numpy 数组的大小调整为 (250,250,13)。

下面的代码适用于 2d 数组,但我无法使其适用于 3d 数组。

>>> a = np.arange(6)
>>> a = a.reshape((2, 3))
>>> np.lib.pad(a, [(2,3),(1,1)], 'constant', constant_values=[(0, 0),(0,0)])
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 1, 2, 0],
       [0, 3, 4, 5, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])

>>> zerpads =np.zeros(13)
>>> data1=np.lib.pad(data,[(73,74),(106,107)],'constant',constant_values=[(zerpads, zerpads),(zerpads,zerpads)])
Traceback (most recent call last):
  File "<stdin>", line …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy zero-padding

12
推荐指数
1
解决办法
1万
查看次数

类型转换和位操作之谜

#include <stdio.h>
#include <stdint.h>
typedef unsigned char       uint8_t;
typedef short               int16_t;
typedef unsigned short      uint16_t;
typedef int                 int32_t;
typedef unsigned int        uint32_t;
int main(){
    uint8_t ball; 
    uint8_t fool;
    ball=((unsigned char)13);
    fool=((unsigned char)14);
    uint16_t combined_value1=((uint16_t)ball)<<12+((uint16_t)fool)<<8; // WRONG ONE
    uint16_t combined_value2=((uint16_t)ball<<12)+((uint16_t)fool<<8);
    printf("%hu\n",(uint16_t)combined_value1);
    printf("%hu\n",(uint16_t)combined_value2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么"combined_value1"的值错了?这里球和傻瓜从0到15取值,我试图将combined_value连接为{ball [4 bits]:傻瓜[4位]:[8个零位]}.

c casting bit-manipulation

2
推荐指数
1
解决办法
91
查看次数

标签 统计

arrays ×1

bit-manipulation ×1

c ×1

casting ×1

numpy ×1

python ×1

zero-padding ×1