0

I am going to simulate a 2d area using a matrix in python and I should change the indexes somehow!. I need to find a relation between the original matrix and a new axis coordinate. I think it can be more clarified if I explain by a piece of my code:

originalMatrix=np.random.randint(0, 10, size=(5,5))

the output is:

[[4 8 3 2 5]
 [2 2 2 6 5]
 [2 4 7 9 9]
 [6 2 6 6 6]
 [2 8 3 8 2]]

we can access to number '7' by originalMatrix[2][2]. but in the coordinate system the index must be (0,0).Index of '4' in the left side of '7' in originalMatrix is [2][1] but in the new coordinate should be (-1,0) and so on... I hope I could explain well!

3
  • Just to clarify, you want the new coordinate system to be relative to the center of the original coordinate system, right? Or is the logic something else? Commented Jan 23, 2022 at 5:35
  • What happens if you have a n x n matrix where n is even? How do you determine the (0,0) coordinate in this case? Commented Jan 23, 2022 at 5:45
  • Actually I need to move (trace) step by step. and the center element is my starting point. so I need to start from (0,0).... Commented Jan 23, 2022 at 5:57

3 Answers 3

2

An easy way is to roll your array:

a = np.array([[4, 8, 3, 2, 5],
              [2, 2, 2, 6, 5],
              [2, 4, 7, 9, 9],
              [6, 2, 6, 6, 6],
              [2, 8, 3, 8, 2]])
center = np.array(a.shape)//2
# b will be indexed with the previous center being 0,0
b = np.roll(a, -center, axis=(0,1))

b[0, -1]
# 4

b[0, 0]
# 7

NB. You have to decide what is the center in case you have an even number as a dimension.

Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't work for b[2,-1], b[0,-2],...
@mohamad How does it not work? It does, work perfectly(b[2,-1] is 8 as expected), but keep in mind that numpy indexing is b[row, column], if you want the other way around use the transpose b.T
shouldn't be b[2,-1] equal to 6? '7' is located at (0,0) so '6' should be located at (2,-1). Am I right?
There are many 6, so the question is ambiguous, but assuming the last 6 of the fourth row in the original data, it is one row after and two columns after the 7, so: b[1, 2] NOT b[-1, 2] that is one row before and two columns after (so a 8). But as I said, if you want a [column, row] indexing, use b = b.T
Also note that row indexing starts from the top, but you can use flipud to flip the array up/down
0

Here is another possible approach, assuming the matrix is nxn where n is odd:

import numpy as np


def access_coordinate(matrix, x, y):
    center = (len(matrix)//2, len(matrix)//2)
    translated_coordinates = (center[0]+y, center[1] + x)
    return matrix[translated_coordinates[0], translated_coordinates[1]]
    

originalMatrix=np.array([[4, 8, 3, 2, 5],
 [2, 2, 2 ,6, 5],
 [2, 4, 7, 9, 9],
 [6, 2, 6, 6, 6],
 [2, 8, 3, 8, 2]])


print(access_coordinate(originalMatrix, -1, 0)) #4

Comments

0

There is no way to do that with standard numpy arrays, but you could define a function that does it for you

def arrayByCoordinates(array, coords):
    coords = np.array(coords)
    center = np.array(array.shape) // 2
    return array[tuple(coords + center)]
>>> x
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
>>> arrayByCoordinates(x,(0,0))
12

But keep in mind that if the shape of the array is even, there is no real center, so this will convert to the lowest one

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.