且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

相当于Python中的MATLAB ind2sub

更新时间:2022-01-15 15:28:14

Google搜索将我引导至此链接:

A Google search lead me to this link: https://github.com/jjakeman/pyheat/blob/master/utilities/math_utils.py

据我所知,MATLAB中没有这些函数的直接实现.

事实证明我无法正确阅读文档.如果要使用sub2ind的功能,则需要 ravel_multi_index 函数.函数声明说您需要两个输入.第一个输入是2D numpy数组,其中每一行都是特定维度的位置.例如,如果要在2D矩阵上应用ind2sub,则应指定2D numpy数组,其中第一行包含所需的所有行位置,第二行包含所有列位置你要.第二个输入是元组,它确定每个维的大小,因此对于2D数组,它是行数和列数.

Just turns out I can't read the documentation properly. If you want the functionality of sub2ind, you'll want the ravel_multi_index function. The function declaration says that you require two inputs. The first input is a 2D numpy array where each row are the locations for a particular dimension. For example, if you wanted to apply ind2sub on a 2D matrix, you would specify a 2D numpy array where the first row consists of all of the row locations you want, and the second row consists of all of the column locations you want. The second input is tuple that determines the size of each dimension, so for a 2D array, it'd be the number of rows and columns.

要执行ind2sub,您需要 unravel_index 函数.第一个输入是一个线性索引数组,您希望将其转换为数组中每个维度的位置.第二个输入是一个像以前一样的尺寸元组.

To do ind2sub, you'd want the unravel_index function. The first input is an array of linear indices that you want converted into locations of each dimension in your array. The second input is a tuple of dimensions like previously.

如果您想自己实现这些功能,我将在后边留下该帖子以供后代使用.

I'm going to leave the post at the bottom for posterity, in case you want to try and implement these yourself.

但是,您当然可以自己实现这些.我假设这是因为您用numpy标记了您的帖子,所以您需要一个numpy式的解决方案.请记住,在numpy中,您访问的是 row 专业而不是 column 专业的元素,因此给定两个数组,一个数组用于行,另一个数组用于列索引(0 -indexed),对于2D矩阵,sub2ind非常简单:

However, you can certainly implement those yourself. I'm assuming that because you tagged your post with numpy that you'll want a numpy-esque solution. Remember, in numpy you access elements in row major, not column major, and so given two arrays such that one is for row and the other for column indices (0-indexed), sub2ind for 2D matrices is very simply:

def sub2ind(array_shape, rows, cols):
    return rows*array_shape[1] + cols

array_shape是两个元素组成的数组,其中第一个元素是矩阵中的行数,第二个元素是列数.如果您还记得的话,可以通过以下方式访问以行为主的矩阵中的元素:

array_shape is an array of two elements where the first element is the number of rows in the matrix and the second element is the number of columns. If you recall, you can access an element in a row-major matrix by:

ind = r*cols + c

(r,c)是所需的行索引和列索引,前提是索引为0.相反,您可以使用整数除法和模数:

(r,c) are the row and column index you want, provided it's 0-indexed. To go the opposite way, you would use integer division and the modulus:

def ind2sub(array_shape, ind):
    rows = (ind.astype('int') / array_shape[1])
    cols = (ind.astype('int') % array_shape[1]) # or numpy.mod(ind.astype('int'), array_shape[1])
    return (rows, cols)

在这里,输出是一个由两个元素组成的元组,其中第一个元素是行位置,第二个元素是列位置.总结ind2sub,要访问所需的行,请使用线性索引并对列进行整数除法.要获得所需的列,请找到模数或余数.转到3维及更高版本要复杂一些.我会让您看一下上面提到的链接以了解更多详细信息.

Here, the output is a two-element tuple where the first element is the row locations and the second element is the column locations. To summarize ind2sub, to access the row you want, you take the linear index and do an integer division with the columns. To get the column you want, you find the modulus or the remainder. Going to 3 dimensions and onwards is a bit more complicated. I'll leave you to take a look at the link I referred above for more details.

很显然,我没有在上述函数中进行任何错误检查,因此在这种情况下,您显然会使用array_shape来发挥自己的优势.做您想要的事的更好方法是:

Obviously, I didn't place any error checking in the above functions, so you'd obviously use array_shape to your advantage in that case. A better way of doing what you want would be something like:

def sub2ind(array_shape, rows, cols):
    ind = rows*array_shape[1] + cols
    ind[ind < 0] = -1
    ind[ind >= array_shape[0]*array_shape[1]] = -1
    return ind

def ind2sub(array_shape, ind):
    ind[ind < 0] = -1
    ind[ind >= array_shape[0]*array_shape[1]] = -1
    rows = (ind.astype('int') / array_shape[1])
    cols = ind % array_shape[1]
    return (rows, cols)

我做了一些基本的错误检查,以确保没有sub2ind的行或列或ind2sub的线性索引超出范围.我将这些位置设置为-1,这样您就知道自己搞砸了.

I did some basic error checking to ensure that no rows or columns for sub2ind or linear indices for ind2sub are out of bounds. I set those locations to -1 so you know you messed up somewhere.

祝你好运!