0

I am a MATLAB user. What is the easiest way to port the following MATLAB script to python

a = []
for i=1:10
    for j=1:10
        a(i,j) =  i*j
    end
end

The question is about dynamically use np.ndarrays. In MATLAB I can initialize a as generical ndarray(via a=[]) without knowing its size, then use the explicit indexing a(i,j). In Python there is the .append method, but when using ndarrays it confuses me, because it would require to pack element of a row first, then pack rows together, or something similar. I would prefer explicit indexing. Is this possible togheter with dynamical arrays or it can be done just with fixed size arrays?

Thanks!

3
  • For loops and dynamic allocation are a bad practice, in Matlab as in Python. Commented Feb 4, 2017 at 13:44
  • Start with a=np.zeros((10,10), dtype=int) if you must iterate like that. Commented Feb 4, 2017 at 15:19
  • MATLAB's arrays aren't actually dynamic. Like numpy arrays, you cannot resize a MATLAB array without creating a new array and copying all the data over. It is just that MATLAB pretends to resize them while numpy doesn't. Commented Feb 4, 2017 at 20:41

1 Answer 1

2

Look at the display when you run the MATLAB

a =  1
a =
   1   2
a =
   1   2   3
a =
   1   2   3   4
.... (so on for 100 iterations)

In Octave I can do:

>> i=1:10
i =

    1    2    3    4    5    6    7    8    9   10

>> j=(1:10)'
j =

    1
    2
    3
    4
    5
    6
    7
    8
    9
   10

>> a=i+j
a =

    2    3    4    5    6    7    8    9   10   11
    3    4    5    6    7    8    9   10   11   12
    4    5    6    7    8    9   10   11   12   13
    5    6    7    8    9   10   11   12   13   14
    6    7    8    9   10   11   12   13   14   15
    7    8    9   10   11   12   13   14   15   16
    8    9   10   11   12   13   14   15   16   17
    9   10   11   12   13   14   15   16   17   18
   10   11   12   13   14   15   16   17   18   19
   11   12   13   14   15   16   17   18   19   20

This makes use of broadcasting, a concept borrowed from numpy

In [500]: i=np.arange(1,11)
In [501]: a = i[:,None] + i
In [502]: a
Out[502]: 
array([[ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
       [ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12],
       [ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13],
       [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
       [ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15],
       [ 7,  8,  9, 10, 11, 12, 13, 14, 15, 16],
       [ 8,  9, 10, 11, 12, 13, 14, 15, 16, 17],
       [ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])

This is best practice - in numpy and I dare say MATLAB and Octave.

But if you must use iteration do something like

In [503]: a=np.zeros((10,10),int)
In [504]: for i in range(10):
     ...:     for j in range(10):
     ...:         a[i,j]=i+j

Or with full blown python list iteration:

In [512]: alist = []
In [513]: for i in range(10):
     ...:     sublist=[]
     ...:     for j in range(10):
     ...:         sublist.append(i+j)
     ...:     alist.append(sublist)
     ...:     
In [514]: alist
Out[514]: 
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
 [2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
 [4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
 [5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
 [6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
 [7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
 [8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
 [9, 10, 11, 12, 13, 14, 15, 16, 17, 18]]
In [515]: np.array(alist)
Out[515]: 
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10],
       [ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
       [ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12],
       [ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13],
       [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
       [ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15],
       [ 7,  8,  9, 10, 11, 12, 13, 14, 15, 16],
       [ 8,  9, 10, 11, 12, 13, 14, 15, 16, 17],
       [ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]])

but I can generate alist more compactly with

alist=[[i+j for i in range(10)] for j in range(10)]

When you build a list of lists, make sure the sublists all have the same length - or else you'll come back to SO with question.

Sign up to request clarification or add additional context in comments.

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.