- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个方形数组x
,形状为(N, N)
,我想检索形状为(n, n)的方形子数组)
,它们以 x
的主对角线为中心。例如,使用 N = 3
& n = 2
,并操作
x = np.arange(9).reshape((3, 3))
应该产生
array([[[0, 1],
[3, 4]],
[[4, 5],
[7, 8]]])
一种方法是使用make_windows
def make_windows(a, sub_w, sub_h):
w, h = a.shape
a_strided = np.lib.stride_tricks.as_strided(
a, shape=[w - sub_w + 1, h - sub_h + 1,
sub_w, sub_h],
strides=a.strides + a.strides)
return a_strided
并执行类似 np.einsum('ii...->i...', make_windows(x, 2, 2))
的操作,但这样做会很简洁一步。单独使用 as_strided
是否可行?
最佳答案
当然:
def diag_windows(x, n):
if x.ndim != 2 or x.shape[0] != x.shape[1] or x.shape[0] < n:
raise ValueError("Invalid input")
w = as_strided(x, shape=(x.shape[0] - n + 1, n, n),
strides=(x.strides[0]+x.strides[1], x.strides[0], x.strides[1]))
return w
例如:
In [14]: x
Out[14]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In [15]: diag_windows(x, 2)
Out[15]:
array([[[ 0, 1],
[ 4, 5]],
[[ 5, 6],
[ 9, 10]],
[[10, 11],
[14, 15]]])
In [16]: diag_windows(x, 3)
Out[16]:
array([[[ 0, 1, 2],
[ 4, 5, 6],
[ 8, 9, 10]],
[[ 5, 6, 7],
[ 9, 10, 11],
[13, 14, 15]]])
In [17]: diag_windows(x, 4)
Out[17]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]]])
关于python - 使用 numpy as_strided 检索以主对角线为中心的子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47861214/
我有一个 6x6 数组,并且希望始终获取接下来的四个值。举个例子: 0----- -1---- --2--- ---3-- ----4- 所以我想得到所有对角线的 (0+1+2+3) 和 (1+2+3
我想遍历一个已转换为一维的二维方阵。 问题是我想遍历它,就像我在对角条中遍历原始 2D 一样。 该数组是对角数组,我最初使用一维的 malloc 创建它,以避免分配太多内存。 数组的大小: int T
“给定一个 2D 字符数组和一个字符串。 查找特定字符串是否出现在矩阵的对角线上。 private static boolean diagonalContains(char[][] grid,Stri
我有一个由 nxn 矩阵组成的 njxnj 矩阵。我想提取 nxn 矩阵的对角 j block 。即我想提取对角线(对于 n = 2,j = 4): 最有效的方法是什么? 最佳答案 要为元素建立索引,
这是一个 self 回答的问题。给定两个数据框, x 0 1 0 1 2 1 3 4 y 0 1 2 0 5 6 7 1 8 9 X 2 Y Z 0 x 和
我试图让我的程序打印出不等于幻方规则的行、列或对角线,例如,如果矩阵是 1 9 5 2 4 3 6 8 7 第 1 行 [2, 4, 3] 不起作用 第 2 行 [6, 8, 7] 不起作用 第 0
所以我有一个像这样的 4x4 矩阵 |0 1 2 3 -+------- 0|0 1 3 6 1|2 4 7 a 2|5 8 b d 3|9 c e f 并且我是按照其中的十六进制字符指定的顺序遍历
什么是获取正方形DataFrame的对角线的有效方法。我希望结果是一个 Series 和一个 MultiIndex 有两个级别,第一个是 DataFrame 的索引,第二个级别是DataFrame 的
问题:我正在尝试在 SwiftUI 中以矩形呈现对角线线性渐变。 我实现了一个标准的多点线性渐变,它在呈现为正方形时效果很好,但是当我将框架更改为矩形时,它有一些奇怪的行为,看起来更水平,或者有一些奇
我目前正在尝试找到一种在 C# for Excel 中使用 VSTO 的方法,以使用 C# 代码在单元格中绘制对角线。但我在网上找不到任何人甚至试图这样做。 有谁知道如何实现这一目标? 谢谢 (对不起
我需要删除图像中的线条,这最终是一个表格。我找到了一种删除水平线和垂直线的方法: convert 1.jpg -type Grayscale -negate -define morphology:co
我有一个如下所示的矩阵: ` matrix = [ ['P', 'o', 'P', 'o', 'P'], ['m', 'i', 'c', 's', 'r'], ['g', 'a', 'T', 'A',
如何在Python中按如下方式堆叠矩阵,使得父矩阵的元素在子矩阵的相同 block 对角点处形成 block 对角线。例子:我有四个矩阵 AA,AB,BA,BB 我想制作如附图所示的矩阵。 最佳答案
我在做一些统计。 我有数据框: tag a b c d e f a 5 2 3 2 0 1 b 2 4 3 2 0 1
我最近做了一个 Connect4 游戏,当我的 Connect4 向右对角线连接时,我的 Connect4 没有赢得游戏。并且它只适用于某些组合,当它连接到左边的对角线时。坐标:- 左上角:(0,0)
在 numpy 中有什么方法可以获取对角数组的引用吗?我希望我的数组对角线除以某个因子谢谢 最佳答案 如果 X 是你的数组,c 是因子, X[np.diag_indices_from(X)] /= c
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我有一个形状为 (m*n, m*n) 的张量,我想提取一个大小为 (n, m*n) 的张量,其中包含对角线上大小为 n*n 的 m 个块。例如: >>> a tensor([[1, 2, 0, 0],
我目前正在使用 matplotlib/pyplot 绘制 3d 对象,如下所示: fig = plt.figure().gca(projection='3d') plot = fig.plot_sur
好的,让我们考虑一个 64 位的数字,它的位组成一个 8x8 的表。 例如 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 0 1 1 0 1 0 1
我是一名优秀的程序员,十分优秀!