2

不明な行数の 2 次元配列を生成する、peaksdetect() などの関数があります。3 としましょう。この 3 つの配列から 1 つの 3-D 配列を作成したいと思います。これが私のスタートですが、多くのifステートメントで非常に複雑なので、可能であれば物事を簡単にしたいと考えています:

import numpy as np

dim3 = 3   # the number of times peaksdetect() will be called
           # it is named dim3 because this number will determine 
           # the size of the third dimension of the result 3-D array

for num in range(dim3):
    data = peaksdetect(dataset[num])            # generates a 2-D array of unknown number of rows
    if num == 0:
        3Darray = np.zeros([dim3, data.shape])  # in fact the new dimension is in position 0
                                                # so dimensions 0 and 1 of "data" will be 
                                                # 1 and 2 respectively
    else:
        if data.shape[0] > 3Darray.shape[1]:
            "adjust 3Darray.shape[1] so that it equals data[0] by filling with zeroes"
            3Darray[num] = data
        else:
            "adjust data[0] so that it equals 3Darray.shape[1] by filling with zeroes"
            3Darray[num] = data
...
4

1 に答える 1

2

配列のサイズを変更しなければならないことを期待している場合、配列を事前に割り当てても得られるものはほとんどありません。配列をリストに格納し、配列のサイズを計算してすべてを保持し、データをそこにダンプする方がおそらく簡単です。

data = []
for num in range(dim3):
    data.append(peaksdetect(dataset[num]))
shape = map(max, zip(*(j.shape for j in data)))
shape = (dim3,) + tuple(shape)
data_array = np.zeros(shape, dtype=data[0].dtype)
for j, d in enumerate(data):
    data_array[j, :d.shape[0], :d.shape[1]] = d
于 2013-04-20T20:39:30.003 に答える