简体   繁体   中英

Is there a method to load a 2D array into three 1D arrays?

Is there a more efficient way or even a method to load a 2D numpy array data[n,m] into three 1D arrays X[n*m] , Y[n*m] , and Z[n*m] than looping over the indices? What I did is:

n = len(data[:,0])
m = len(data[0,:])
X = zeros(n*m)
Y = zeros(n*m)
Z = zeros(n*m)
c = 0
for i in range(n):
    for j in range(m):
        X[c] = i
        Y[c] = j
        Z[c] = data[i,j]
        c += 1

If your codes actually does what you intended. This should be the equivalent.

X,Y =  np.indices(data.shape)
Z = data.ravel()
X = X.ravel()
Y = Y.ravel()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM