如何在numpy中解交织数组? [英] How to de-interleave array in numpy?

查看:95
本文介绍了如何在numpy中解交织数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以棘手的方式交错的numpy数组,我想不出一种简单的方法来对其进行解交错.假设(84,132)矩阵为:

I have a numpy array that is interleaved in a tricky way and I can't figure out an easy way to de-interleave it. Suppose the (84, 132) matrix is:

0   100  200 ...
1   101  201 ...
2   102  202 ...
...
83  183  283 ...

我想从第一列中获取所有第四个元素,然后从第二行开始获取每个第四元素,然后从第三行开始获取每个第四元素,然后从第四行开始获取每个第四元素.(产生四个新列.)然后,我想对第二列重复,以此类推.所以我想要的(21,528)结果是:

I want to take every fourth element from the first column, then every fourth element starting from the second row, then every fourth starting from the third row, then every fourth starting from the fourth row. (Yielding four new columns.) Then I want to repeat for the second column, and so forth. So the (21, 528) result I want is:

0   1  2  3 100 101 102 103 200 ...
4   5  6  7 104 105 106 107 204 ...
8   9 10 11 108 109 110 111 208 ...
...
80 81 82 83 180 181 182 183 283 ...

我可以循环执行此操作,将(84,132)数组 a 转换为(21,528)数组 b :

I can do this with a loop, converting the (84, 132) array a to a (21, 528) array b:

b = np.zeros(shape=(21, 132*4))
for y in range(0, 21):
  for x in range(0, 132):
    for s in range(0, 4):
      b[y, x * 4 + s] = a[y * 4 + s, x]

是否有更好的方法来执行numpy操作?

Is there a nicer way to do this with numpy operations?

(上下文:这是8086处理器中微代码ROM的物理排列,我正在尝试对这些位进行混洗以进行分析.)

(Context: this is the physical arrangement of the microcode ROM in the 8086 processor and I'm trying to unshuffle the bits for analysis.)

推荐答案

置换轴并使用借来的想法进行重塑 Generalnd到nd转换的想法 .-

Permute axes and reshape with the idea being borrowed off General idea for nd to nd transformation. -

N = 4 # number of rows to split with
n = a.shape[1]
a.reshape(-1,N,n).swapaxes(1,2).reshape(-1,n*N)

这篇关于如何在numpy中解交织数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆