python - Shift rows in array independently
问题描述
I want to shift each row by its row-number and be relative to my desired output shape. An example:
array([[0, 1, 2], array([[0, 1, 2], array([[0, 1, 2, 0, 0],
[1, 2, 3], -> [1, 2, 3], -> [0, 1, 2, 3, 0],
[2, 3, 4]]) [2, 3, 4]]) [0, 0, 2, 3, 4])
The array to furthest left is my input and the array to the furthest right is my desired output. This can be generalized to bigger arrays, for example a 10x10
array.
Is there a nice way of doing this?
What I have is:
A = np.array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5]], dtype=np.float32)
out = np.zeros((A.shape[0], A.shape[1]*2-1))
out[[np.r_[:3]], [np.r_[:3] + np.r_[:3][:,None]]] = A
解决方案
Here is a solution for square matrices of size n.
np.concatenate((A,np.zeros((n,n))),axis=1).flatten()[0:-n].reshape([n,2*n-1])
推荐阅读
- rust - 有没有办法在不导入其他类型的情况下将模块中的所有特征带入范围?
- swift - 使用 DispatchGroup 时如何超时?
- css - Reac-Bootstrap如何覆盖表单输入背景
- javascript - React 菜单最佳实践
- android - 当我尝试创建 apk 时,颤振卡在“正在运行 Gradle 任务 'bundleRelease'...”
- javascript - 猫鼬数组长度函数返回对象类型
- java - 为什么有内部服务器错误,无法在 /bin/searchServlet 创建节点
- r - 从 xml 文件中提取数据时,如何从示例字符串“2019-07-02T06:05:00”中删除字母 T?
- python - 尝试使用 BeautifulSoup 或 Xpath 抓取时无法找到 page_source 中的元素
- sql - 通过 ID 识别从日期到 Null 的更改