9

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?

1
  • 3
    Use NumPy. It's like Matlab in Python. Commented Jan 19, 2017 at 12:06

3 Answers 3

11

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.]
Sign up to request clarification or add additional context in comments.

4 Comments

Perhaps you should also show the import statement since you use np and it makes your answer "more canned". +1.
Also note that in MATLAB one generally indexes A(x,y,z) while python follows A[z,y,x] when doing maths
Yes, difference in the parenthesis and the brackets, but the indexing styles are same, also matlab uses 1-based indexing like R instead of python's 0-based indexing.
This looks more like it! Thank you!
11

A pure Python way to do this is using a list of lists (or in this case a list of lists of lists). You can initialize it with list comprehension. For instance:

w = 4 #width
h = 3 #height
d = 3 #depth

data = [[[0]*h for _ in range(w)] for _ in range(d)]

Or if you want to fill the tensor with tuples like on the figure:

data = [[[(i+1,j+1,k+1) for k in range(h)] for j in range(w)] for i in range(d)]

This initializes a d x w x h "matrix" filled with zeros.

You can then access the (i,j,k)-th element with:

data[i][j][k]

Nevertheless there are libraries like that have support for vectors, matrices, tensors, etc.

2 Comments

Ok, thanks for your answer! Then I guess that what I am trying to do is a list of arrays, something like A = [array([[1,2,3],[4,5,6]]), array([[5,6,7], [7,8,9]])] However, is there a way to call only the first elements of each row of the first array in A? Something that in matlab would be A(:,1,1). Thanks!
@S88S: Not as far as I know. In that case you better use numpy: numpy is really meant for doing matrix,... calculations whereas Python itself is of course a general purpose programming language.
2

If you're willing to use NumPy then there's plenty of ways. One way would be to initialise with all zeros or, as in your updated example, you could also fill with a range and then reshape.

import numpy as np

a = np.arange(48, dtype=np.int64).reshape((3, 4, 4))
# or 
b = np.zeros((3, 4, 4), dtype=np.int64)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.