-1

I have an array which is 1 -> 160. I want to split this into 10 arrays that are split every sixteen numbers. This is what I have so far:

amplitude=[]
for i in range (0,160):
    amplitude.append(i+1)

print(amplitude)


#split arrays up into a line for each sample
traceno=10                  #number of traces in file
samplesno=16               #number of samples in each trace. This wont change.

amplitude_split=np.zeros((traceno,samplesno) ,dtype=np.int) 


#fill in the arrays with amplitude/sample numbers
for i in range(len(amplitude)):             
    for j in range(traceno):                
        for k in range(samplesno):          
            amplitude_split[j,k]=amplitude[i]  

print(amplitude_split[1,:])

As an output I only get [160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160] Where I require something along the lines of:

[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]

[17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32] etc...

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
okvoyce
  • 153
  • 2
  • 10
  • 1
    [how-do-you-split-a-list-into-evenly-sized-chunks](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – Patrick Artner Apr 15 '20 at 08:40
  • oops I copied the wrong bit over... it should say amplitude[] – okvoyce Apr 15 '20 at 08:43
  • 2
    maybe use: `np.array_split(range(1,161), 10))` (found [here](https://stackoverflow.com/a/16935535/9758194)) – JvdV Apr 15 '20 at 08:45
  • @PatrickArtner for vanilla Python, yes, but this appears to be a Numpy question. – Karl Knechtel Apr 15 '20 at 08:49
  • 1
    @Karl Thats why I did not dupe hammer - the only hint that this is numpy is the usage of np.zero in the code. chunking 160 elements and a dependency on numpy is only ok if you use numpy already imho - edited the tags to emphasize numpy – Patrick Artner Apr 15 '20 at 08:51

3 Answers3

2

You are nesting the loops. So you consistently fill the new array with the same number from the first one, and end with the last one 160 repeated everywhere.

You only need to copy the list into a 1D numpy array, and then reshape it:

amplitude_split=np.array(amplitude, dtype=np.int).reshape((traceno,samplesno))
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

Well, if we're using Numpy arrays, we can use Numpy functionality:

amplitude = np.arange(1, 161)
amplitude_split = amplitude.reshape(10, 16)

Otherwise, you've already been linked to how to do it for plain lists, but I'd like to point out that you still don't need a loop to fill amplitude in the first place:

amplitude = list(range(1, 161))

In general, with Python you should be trying hard not to think in terms of starting with an initially blank "storage" area that you then fill in. Just create the data you want directly - by conversions of the sort above, by list comprehensions etc., or if necessary by .append() ing - rather than overwriting a dummy value.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Thanks, this has worked! I know about the filling in a list, I was just writing a test for a different piece of code I was writing which was too long to run each time (an array of 16000000 numbers) – okvoyce Apr 15 '20 at 08:55
0

See grouper in https://docs.python.org/2/library/itertools.html#recipes

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)
log0
  • 10,489
  • 4
  • 28
  • 62