简体   繁体   English

用python / numpy转换为向量的矩阵

[英]Matrix to Vector with python/numpy

Numpy ravel works well if I need to create a vector by reading by rows or by columns. 如果我需要按行或按列创建矢量,Numpy ravel效果很好。 However, I would like to transform a matrix to a 1d array, by using a method that is often used in image processing. 但是,我想通过使用图像处理中经常使用的方法将矩阵转换为一维数组。 This is an example with initial matrix A and final result B : 这是初始矩阵A和最终结果B的示例:

A = np.array([[ 0,  1,  2,  3],
              [ 4,  5,  6,  7],
              [ 8,  9, 10, 11],
              [12, 13, 14, 15]])

B = np.array([[ 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15])

Is there an existing function already that could help me with that? 已经有一个现有的功能可以帮助我吗? If not, can you give me some hints on how to solve this problem? 如果没有,您能给我一些如何解决这个问题的提示吗? PS. PS。 the matrix A is NxN . 矩阵ANxN

I've been using numpy for several years, and I've never seen such a function. 我已经使用numpy几年了,但从未见过这样的功能。

Here's one way you could do it (not necessarily the most efficient): 这是一种方法(不一定是最有效的方法):

In [47]: a
Out[47]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

In [48]: np.concatenate([np.diagonal(a[::-1,:], k)[::(2*(k % 2)-1)] for k in range(1-a.shape[0], a.shape[0])])
Out[48]: array([ 0,  1,  4,  8,  5,  2,  3,  6,  9, 12, 13, 10,  7, 11, 14, 15])

Breaking down the one-liner into separate steps: 将单线分解为单独的步骤:

a[::-1, :] reverses the rows: a[::-1, :] ,: a[::-1, :]反转行:

In [59]: a[::-1, :]
Out[59]: 
array([[12, 13, 14, 15],
       [ 8,  9, 10, 11],
       [ 4,  5,  6,  7],
       [ 0,  1,  2,  3]])

(This could also be written a[::-1] or np.flipud(a) .) (也可以将其写为a[::-1]np.flipud(a) 。)

np.diagonal(a, k) extracts the k th diagonal, where k=0 is the main diagonal. np.diagonal(a, k)提取第k个对角线,其中k=0是主对角线。 So, for example, 因此,例如

In [65]: np.diagonal(a[::-1, :], -3)
Out[65]: array([0])

In [66]: np.diagonal(a[::-1, :], -2)
Out[66]: array([4, 1])

In [67]: np.diagonal(a[::-1, :], 0)
Out[67]: array([12,  9,  6,  3])

In [68]: np.diagonal(a[::-1, :], 2)
Out[68]: array([14, 11])

In the list comprehension, k gives the diagonal to be extracted. 在列表推导中, k给出要提取的对角线。 We want to reverse the elements in every other diagonal. 我们要反转每个对角线中的元素。 The expression 2*(k % 2) - 1 gives the values 1, -1, 1, ... as k varies from -3 to 3. Indexing with [::1] leaves the order of the array being indexed unchanged, and indexing with [::-1] reverses the order of the array. 表达式2*(k % 2) - 1给出值1,-1,1,...,因为k在-3到3之间变化。用[::1]索引使数组的索引顺序保持不变,使用[::-1]索引将反转数组的顺序。 So np.diagonal(a[::-1, :], k)[::(2*(k % 2)-1)] gives the k th diagonal, but with every other diagonal reversed: 所以np.diagonal(a[::-1, :], k)[::(2*(k % 2)-1)]给出第k个对角线,但其他对角线都被反转了:

In [71]: [np.diagonal(a[::-1,:], k)[::(2*(k % 2)-1)] for k in range(1-a.shape[0], a.shape[0])]
Out[71]: 
[array([0]),
 array([1, 4]),
 array([8, 5, 2]),
 array([ 3,  6,  9, 12]),
 array([13, 10,  7]),
 array([11, 14]),
 array([15])]

np.concatenate() puts them all into a single array: np.concatenate()将它们全部放入一个数组中:

In [72]: np.concatenate([np.diagonal(a[::-1,:], k)[::(2*(k % 2)-1)] for k in range(1-a.shape[0], a.shape[0])])
Out[72]: array([ 0,  1,  4,  8,  5,  2,  3,  6,  9, 12, 13, 10,  7, 11, 14, 15])

I found discussion of zigzag scan for MATLAB, but not much for numpy . 我发现了有关MATLAB的之字形扫描的讨论,但没有涉及numpy One project appears to use a hardcoded indexing array for 8x8 blocks 一个项目似乎使用8x8块的硬编码索引数组

https://github.com/lot9s/lfv-compression/blob/master/scripts/our_mpeg/zigzag.py https://github.com/lot9s/lfv-compression/blob/master/scripts/our_mpeg/zigzag.py

ZIG = np.array([[0,  1,  5,  6,  14, 15, 27, 28],
               [2,  4,  7,  13, 16, 26, 29, 42],
               [3,  8,  12, 17, 25, 30, 41, 43],
               [9,  11, 18, 24, 31, 40, 44,53],
               [10, 19, 23, 32, 39, 45, 52,54],
               [20, 22, 33, 38, 46, 51, 55,60],
               [21, 34, 37, 47, 50, 56, 59,61],
               [35, 36, 48, 49, 57, 58, 62,63]])

Apparently it's used jpeg and mpeg compression. 显然,它使用了jpeg和mpeg压缩。

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

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