Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define multidimensional arrays in python?

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;

enter image description here

Does anybody know how can I define the same structure in python?

like image 632
S88S Avatar asked Jan 19 '17 12:01

S88S


1 Answers

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.]
like image 109
Sandipan Dey Avatar answered Sep 19 '22 04:09

Sandipan Dey