Home > other >  listing all the values inside a matrix into a 1 whole list
listing all the values inside a matrix into a 1 whole list

Time:04-18

How can I put all the values of my matrix ckpot_p_shell_matrix into just 1 whole list? I want the values in a list so I can do a histogram plot of the values.

ckpot_p_shell_matrix is a numpy.ndarray 2D matrix that has a shape (28, 108) containing values between 0 ~ 10.

>>> type(ckpot_p_shell_matrix)
    numpy.ndarray
>>> ckpot_p_shell_matrix.shape
    (28, 108)
>>> ckpot_p_shell_matrix
    array([[0.2407545, 0.3681921, 0.5176657, ..., 2.9999998, 1.5      ,
        2.9316723],
       [0.       , 0.       , 0.       , ..., 0.       , 0.       ,
        0.       ],
       [0.       , 0.       , 0.       , ..., 0.       , 0.       ,
        0.       ],
       ...,
       [3.5468006, 2.0326045, 2.42928  , ..., 4.5      , 5.25     ,
        3.5797157],
       [0.7088   , 1.5522   , 1.0474   , ..., 4.       , 3.       ,
        3.95444  ],
       [5.2912   , 4.4478   , 4.9526   , ..., 6.       , 7.       ,
        6.04556  ]])

CodePudding user response:

Use numpy .flatten()

import numpy as np

ckpot_p_shell_matrix = np.array([[0.2407545, 0.3681921, 0.5176657, ..., 2.9999998, 1.5      ,
        2.9316723],
       [0.       , 0.       , 0.       , ..., 0.       , 0.       ,
        0.       ],
       [0.       , 0.       , 0.       , ..., 0.       , 0.       ,
        0.       ],
       ...,
       [3.5468006, 2.0326045, 2.42928  , ..., 4.5      , 5.25     ,
        3.5797157],
       [0.7088   , 1.5522   , 1.0474   , ..., 4.       , 3.       ,
        3.95444  ],
       [5.2912   , 4.4478   , 4.9526   , ..., 6.       , 7.       ,
        6.04556  ]])
print(ckpot_p_shell_matrix.flatten())

Output:

np.array([[0.2407545, 0.3681921, 0.5176657, ..., 2.9999998, 1.5      ,

CodePudding user response:

you could use the .flatten() method and then convert it to a list using the builtin list() function

result=list(ckpot_p_shell_matrix.flatten())

if you try to print it, the buffer will just stop at some point and not display the rest.

CodePudding user response:

You can use flatten() as mentioned above, or reshape(-1):

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

print(a.flatten())
print(a.reshape(-1))

output:

[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
  • Related