In MATLAB there is an easy way to define multidimensional arrays e.g.
A(:,:,1) = [1,2,3; 4,5,6];
A(:,:,2) = [7,8,9; 10,11,12];
>> A
A(:,:,1) =
1 2 3
4 5 6
A(:,:,2) =
7 8 9
10 11 12
where the first two indices are respectively, for the rows and columns of the ith matrix (or page, see picture below) stored in A;
Does anybody know how can I define the same structure in python?
with NumPy indexing is similar to MATLAB
import numpy as np
A=np.empty((2,3,3))
A.shape
#(2L, 3L, 3L)
A[0,1,2] # element at index 0,1,2
#0.0
A[0,:,:] # 3x3 slice at index 0
#array([[ 0., 0., 0.],
# [ 0., 0., 0.],
# [ 0., 0., 0.]])
A[1,1,:] # 1-D array of length 3
#array([ 0., 0., 0.]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With