user2221667
user2221667

Reputation: 144

Numpy reshaping array

I have an array reproduced below, when I plot it I get a sawtooth wave, I am looking for a square wave output, ie when the value the 11th value in the second column should decrease in value. I am looking for a way to do this without manually reshaping everytime.

For example I have this:

[[ 0.   0. ]
 [ 0.   0.1]
 [ 0.   0.2]
 [ 0.   0.3]
 [ 0.   0.4]
 [ 0.   0.5]
 [ 0.   0.6]
 [ 0.   0.7]
 [ 0.   0.8]
 [ 0.   0.9]
 [ 0.1  0. ]
 [ 0.1  0.1]
 [ 0.1  0.2]
 [ 0.1  0.3]
 [ 0.1  0.4]
 [ 0.1  0.5]
 [ 0.1  0.6]
 [ 0.1  0.7]
 [ 0.1  0.8]
 [ 0.1  0.9]

I want this:

 [[ 0.   0. ]
 [ 0.   0.1]
 [ 0.   0.2]
 [ 0.   0.3]
 [ 0.   0.4]
 [ 0.   0.5]
 [ 0.   0.6]
 [ 0.   0.7]
 [ 0.   0.8]
 [ 0.   0.9]
 [ 0.1  0.9]
 [ 0.1  0.8]
 [ 0.1  0.7]
 [ 0.1  0.6]
 [ 0.1  0.5]
 [ 0.1  0.4]
 [ 0.1  0.3]
 [ 0.1  0.2]
 [ 0.1  0.1]
 [ 0.1  0.0]

Upvotes: 0

Views: 123

Answers (2)

wflynny
wflynny

Reputation: 18521

If you have a specified ymin, ymax, ystep, you could do something like:

import numpy as np
ymin, ymax, ystep = 0, 1, 0.1
z = np.arange(ymin, ymax, ystep)
x = np.repeat(z, len(z))
y = np.tile(np.tile((z, z[::-1]), (1, 1)).flatten(), len(z)/2)
arr = np.vstack((x, y)).T

>>>arr[:20]
array([[ 0. ,  0. ],
       [ 0. ,  0.1],
       [ 0. ,  0.2],
       [ 0. ,  0.3],
       [ 0. ,  0.4],
       [ 0. ,  0.5],
       [ 0. ,  0.6],
       [ 0. ,  0.7],
       [ 0. ,  0.8],
       [ 0. ,  0.9],
       [ 0.1,  0.9],
       [ 0.1,  0.8],
       [ 0.1,  0.7],
       [ 0.1,  0.6],
       [ 0.1,  0.5],
       [ 0.1,  0.4],
       [ 0.1,  0.3],
       [ 0.1,  0.2],
       [ 0.1,  0.1],
       [ 0.1,  0. ]])
       #keeps going
       #...
       #[0.9,  0.9],
       #...
       #[0.9,  0. ]]

Upvotes: 1

ebarr
ebarr

Reputation: 7842

If the first column is staying the same in the bottom half of the array:

my_array[10:] = my_array[10:][::-1]

Or if your array is not a fixed size:

my_array[my_array.shape[0]/2:] = my_array[my_array.shape[0]/2:][::-1]

Upvotes: 1

Related Questions