将矩阵矢量化与z阶

发布于 2025-01-29 13:37:06 字数 1084 浏览 3 评论 0原文

我有一个二维正方形阵列,我想在。 有没有有效的方法?

一点上下文:我的矩阵是一个图像 u ,我平均每个2x2单元格,以获取一个图像,其图像是原始图像的一半。如果我的图像在z订单之后将我的图像矢量化为 v ,我可以将此操作写为 rv ,其中 r 是一个矩阵,很容易写(与必须对 v'行动的 r'相比,该是由经典的行订单矢量化的),我需要这以备份从理论上讲我的代码。

编辑:由于我的问题还不够清楚,因此我添加了一个可以执行我想要的代码,可能以不优化的方式。

import numpy as np

def z_order(M,p):
 if p==0:
  return [M[0,0]]
 else:
  A = z_order(M[0:2**(p-1) , 0:2**(p-1)] , p-1)
  B = z_order(M[0:2**(p-1) , 2**(p-1):2**p], p-1)
  C = z_order(M[2**(p-1):2**p , 0:2**(p-1)], p-1)
  D = z_order(M[2**(p-1):2**p , 2**(p-1):2**p], p-1)
  return A+B+C+D

如果m现在是一个具有侧面2 ** p的正方形numpy阵列

,则该代码(p = 2)

M = np.array([[9,8,7,6],
    [1,2,3,4],
    [0,1,0,1],
    [5,6,1,2]])

[9, 8, 1, 2, 7, 6, 3, 4, 0, 1, 5, 6, 0, 1, 1, 2]

根据z订单给出了矢量化矩阵

您认为我的代码高效吗?如果没有,您可以想到的最快的方法是什么?

I have a 2-dimensional square array and I would like to vectorize it following a Z-order.
Is there an efficient way to do it?

A little bit of context: my matrix is an image U and I am averaging every 2x2 cell, to get an image that's half the size of the original one. If my image is vectorized as v following the Z-order, I can write this operation as Rv where R is a matrix that is much easier to write (compared to an R' that has to act on a v' that's been vectorized by a classical row-by-row order), and I need this for backing my code theoretically.

Edit: as my question was not sufficiently clear, I am adding a code that would perform what I want, possibly in a non-optimized way.

import numpy as np

def z_order(M,p):
 if p==0:
  return [M[0,0]]
 else:
  A = z_order(M[0:2**(p-1) , 0:2**(p-1)] , p-1)
  B = z_order(M[0:2**(p-1) , 2**(p-1):2**p], p-1)
  C = z_order(M[2**(p-1):2**p , 0:2**(p-1)], p-1)
  D = z_order(M[2**(p-1):2**p , 2**(p-1):2**p], p-1)
  return A+B+C+D

Where M is meant to be a square numpy array with side 2**p

Now, this code applied (with p=2) to

M = np.array([[9,8,7,6],
    [1,2,3,4],
    [0,1,0,1],
    [5,6,1,2]])

gives

[9, 8, 1, 2, 7, 6, 3, 4, 0, 1, 5, 6, 0, 1, 1, 2]

which is the vectorized matrix according to the z-order.

Do you think my code is efficient? If not, what is the fastest way to do this that you can think of?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文