nrcjea001
nrcjea001

Reputation: 1057

Python: Initialize numpy arrays within an array of zeroes

In Python, I am trying to initialize 2-element arrays of zeros within a size N by N array. The code I'm using works but I'm looking for something more efficient and elegant:

array1 = np.empty((N,N), dtype=object)
for i in range(N):
    for j in range(N):
        array1[i,j] = np.zeros(2, dtype=np.int)

Thank ahead for the help

Upvotes: 2

Views: 643

Answers (1)

jeannej
jeannej

Reputation: 1192

As I understand it, you should probably use a 3D array:

import numpy as np
array1 = np.empty((N,N,2), dtype=object)

which returns an array of N rows, N columns and 2 depth. If you want to pass a (NxN) array to let's say the first depth, just use:

tmp = np.ones(N,N) #for instance
array1(:,:,0) = tmp

Upvotes: 3

Related Questions