A tuple of matrices is a collection of elements suitable for indexing. The output will take the form of indexing matrices (or arrays), and each element at the output will be selected from the original array, using the first array as the index of the first dimension, the second as the index of the second dimension, and so on. In other words, it is:
>>> numpy.where(M == 0) (matrix([[0, 0, 4]]), matrix([[0, 2, 1]])) >>> row, col = numpy.where(M == 0) >>> M[row, col] matrix([[0, 0, 0]]) >>> M[numpy.where(M == 0)] = 1000 >>> M matrix([[1000, 1, 1000], [ 4, 2, 4], [ 3, 4, 1], [ 1, 3, 2], [ 2, 1000, 3]])
The sequence may be up to you. It passes in a flattened order - therefore, M[0,2] appears second, not third. If you need to reorder, you can do this:
>>> row[0,col.argsort()] matrix([[0, 4, 0]])
It may also be useful for you to use arrays instead of matrices. This way you can manipulate the shape of arrays, which is often useful! Also note the ajcr trick, which is probably preferable to using argsort .
Finally, there is also a nonzero method that does the same as where in this case. Now use the transpose trick:
>>> (M == 0).T.nonzero() (matrix([[0, 1, 2]]), matrix([[0, 4, 0]]))