0

Assuming a list as below:

Y= [-0.0, -0.0, -0.0, -0.0, -0.0, -0.0, 4.0, 4.0, -0.0, 2.0, -0.0, -0.0, 4.0, -0.0, -0.0, -0.0, -0.0, -0.0, 4.0, 2.0, -0.0, -0.0, -0.0, 4.0, 2.0, 4.0, -0.0, -0.0, -0.0, -0.0, 4.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, 4.0, -0.0, -0.0, 2.0, -0.0, -0.0, -0.0, -0.0, -0.0]

How this list could be converted to a matrix for each n number. let's say set n=10, the matrix or array is supposed to look like below:

Example of expected output

Cheers,

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Sean
  • 3
  • 1

2 Answers2

2

This should do the job. This creates a lists of list though.

step_size = 10
steps = range(0, len(Y), step_size)

y = [Y[step:step + step_size] for step in steps]

If you want an array:

import numpy as np

n = 10

y = np.array(Y).reshape(-1, n)

Yields:

[[-0. -0. -0. -0. -0. -0.  4.  4. -0.  2.]
 [-0. -0.  4. -0. -0. -0. -0. -0.  4.  2.]
 [-0. -0. -0.  4.  2.  4. -0. -0. -0. -0.]
 [ 4. -0. -0. -0. -0. -0. -0. -0. -0. -0.]
 [-0.  4. -0. -0.  2. -0. -0. -0. -0. -0.]]
emremrah
  • 1,733
  • 13
  • 19
-1

Hi what you can do is

list = [ [0,0,0,0,0,0,0,0,0,0,0,],[....],...,[...]] These are nested list where each internal list is a row and 1colume

new_to_code
  • 37
  • 1
  • 12