本文介绍了Python中向量矩阵的相应坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有了答案此处介绍如何使用Matlab获得矢量化矩阵的相应坐标.

I had already an answer here of how to get using Matlab the corresponding coordinates of a vectorized matrix.

我想知道如何将答案更改为Python.Python是否采用相同的概念来存储数组元素,但是不是以1开头,而是以0开头?

I am wondering how can the answer be changed to Python. Does Python apply the same concept of storing the array elements but instead of starting by 1, it starts by 0?

通过应用答案中提到的相同的数组元素存储,并通过在Python中手动执行 sub2ind 来实现:

By applying the same array elements storing as mentioned in the answer, and by performing sub2ind manually in Python, I am doing:

row = np.mod(i,m)
col = np.floor((i)/m)

我正确吗?寻找可以为我提供更多详细信息的人,以防万一我错了.

Am I correct? Looking for someone who can provide me more details in case I am wrong.

推荐答案

Numpy数组以行优先顺序存储,因此您将需要

Numpy arrays are stored in row-major order so you would need

col = np.mod(i,m)
row = np.floor((i)/m)

可以写得更简单

col = i%m
row = i//m

但是,numpy具有 numpy.unravel_index 函数,无需手动计算即可执行此功能.

However, numpy has the numpy.unravel_index function which does this without the need for manual computations.

这篇关于Python中向量矩阵的相应坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:02