在Matlab中,你可以很容易地设置一个带编号的网格,并通过各种索引技巧将操作符映射到“矢量化”的表单上。更明确地编写这个并不难,但我想知道是否有一个numpy方法来实现这一点?也许是vianumpy.nditer?在
这段代码(参见mathworks示例)使用存储在矩阵网格G中的顺序从向量u到矩阵u:
U = G;
U(G>0) = full(u(G(G>0)));
参考文献:
http://www.mathworks.com/products/matlab/examples.html?file=/products/demos/shipping/matlab/delsqdemo.html
Tags:
我不知道你所说的“标签网格”或“编号网格”是什么意思。在
看起来演示依赖于两个函数,}。你可以在MATLAB中查看代码。但在网上我发现
numgrid
和{http://chmielowski.eu/POLITECHNIKA/Dydaktyka/AUTOMATYKA/AutoLab/Matlab/TOOLBOX/MATLAB/DEMOS/NUMGRID.M
http://chmielowski.eu/POLITECHNIKA/Dydaktyka/AUTOMATYKA/AutoLab/Matlab/TOOLBOX/MATLAB/DEMOS/DELSQ.M
日期是1991年,所以它们是用老式的MATLAB编写的,没有更新的“矢量化”或任何花哨的映射技巧(我可以看到)。唯一特别的是使用稀疏矩阵(
delsq
返回稀疏矩阵)。它生成的曲面已经是Mathworks标志很长时间了。在所以将这些转换成{}应该是直接的。要生成稀疏矩阵,您需要
scipy.sparse
。MATLAB
sparse(i,j,s)
=>;scipy.sparse.csr_matrix(s, (i,j))
。在但如果您更关心索引步骤:
对于匹配的
data
数组,以下内容是等效的倍频程(MATLAB):
^{pr2}$numpy
下面复制了演示。
G
中的编号是不同的,但数字只是标签(标签网格让我困惑)。在结果:
也许我误解了,但是你的问题似乎是关于boolean indexing and assignment,而不是与使用
delsq
的特定示例有关,等等为了扩展我的评论,听起来你只是想:(使用
x
而不是u
来避免混淆大写)作为一个更具体的例子,假设您有一个数组(让我们使用这个例子中的一个,ipython的
^{pr2}$%paste x
命令使这个过程更简单):这就产生了:
另一方面,matlab示例处理的是稀疏数组。
scipy.sparse
提供了多种稀疏数组类型,但并非所有类型都支持这种类型的索引。但是,如果处理稀疏数组,只需sparse_array.data[:] = newvals
来更改值,同时保持稀疏结构。在相关问题 更多 >
编程相关推荐