Search code examples
pythonnumpymatrixposition

Printing the positions MATRIX of the values in a matrix


I have a matrix with some values. I want to get the m,n positions of each value into a separate matrix.

import numpy as np

a = np.zeros((3,3)) #Creating "a" matrix with all zeros
vals = [22,34,43,56,37,45] # the values that i want to add to the matrix "a"
pos = [(0,1),(0,2),(1,0),(1,2),(2,0),(2,1)] # Corresponding positions of the above given values

rows, cols = zip(*pos)   # This code is to assign the given values in the correct position

a[rows, cols] = vals

print (a)

output =

[[(0,0) (0,1) (0,2)]

[(1,0) (1,1) (1,2)]

[(2,0) (2,1) (2,2)]]

please help me out.

I want to get the position matrix(m,n) of whatever a matrix (m,n)


Solution

  • As per my understanding use below application code(tested in python 3.6.2):

    import numpy as np
    matrixlist=[]
    tuple1=()
    list1=[]
    list1=tuple(tuple1)
    rows= 3
    cols=3
    
    a = np.zeros((rows*cols))
    for i in range(rows):
        list1=[]
        list1.append(i)
        t1=tuple(list1)
        for j in range(cols):
            list1=list(t1)
            list1.append(j)
            t2=tuple(list1)
            matrixlist.append(t2)
            list1.remove(list1[0])
    data=[]
    data= matrixlist
    i=0
    m=[data[i:i+cols] for i in range(0, len(data), cols)]
    print(m)
    

    OutPUT:

    [[(0, 0), (0, 1), (0, 2)], [(1, 0), (1, 1), (1, 2)], [(2, 0), (2, 1), (2, 2)]]
    

    Hope this will full fill your requirement!