18

I am trying to convert a MATLAB code in Python. I don't know how to initialize empty matrix in Python.

MATLAB Code:

demod4(1) = [];

I tried in Python

demod4[0] = array([])

but it gives error:

only length-1 arrays can be converted to Python scalars
gevang
  • 4,994
  • 25
  • 33
marriam nayyer
  • 677
  • 5
  • 9
  • 19
  • 4
    You'll have to share a bit more about your object `demod4` and your imports. But, there are two options, `numpy.empty` or `numpy.zeros` to initialize an empty or zero array respectively of a given size. – Justin Aug 26 '13 at 17:01
  • By empty, do you mean `size == 0`? – Bi Rico Aug 26 '13 at 17:52
  • your MATLAB code sample will not initialize an empty array, but will remove indexed element `1` (and vectorize your `demod4` matrix as a result). For an empty matrix you need `demod4=[]`. – gevang Aug 26 '13 at 18:14

8 Answers8

42

If you are using numpy arrays, you initialize to 0, by specifying the expected matrix size:

import numpy as np
d = np.zeros((2,3))

>>> d
    [[ 0.  0.  0.]
     [ 0.  0.  0.]]

This would be the equivalent of MATLAB 's:

d = zeros(2,3);

You can also initialize an empty array, again using the expected dimensions/size

d = np.empty((2,3))

If you are not using numpy, the closest somewhat equivalent to MATLAB's d = [] (i.e., a zero-size matrix) would be using an empty list and then

append values (for filling a vector)

d = []
d.append(0)
d.append(1)
>>> d                                                                     
[0, 1]

or append lists (for filling a matrix row or column):

d = []                                                                
d.append(range(0,2))                                                    
d.append(range(2,4))                                                  
>>> d                                                                     
[[0, 1], [2, 3]]

See also:

initialize a numpy array (SO)

NumPy array initialization (fill with identical values) (SO)

How do I create an empty array and then append to it in NumPy? (SO)

NumPy for MATLAB users

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
gevang
  • 4,994
  • 25
  • 33
38

You could use a nested list comprehension:

# size of matrix n x m
matrix = [ [ 0 for i in range(n) ] for j in range(m) ]
darmat
  • 698
  • 4
  • 10
11

What about initializing a list, populating it, then converting to an array.

demod4 = []  

Or, you could just populate at initialization using a list comprehension

demod4 = [[func(i, j) for j in range(M)] for i in range(N)]

Or, you could initialize an array of all zeros if you know the size of the array ahead of time.

demod4 = [[0 for j in range(M)] for i in range(N)]

or

demod4 = [[0 for i in range(M)]*N]

Or try using numpy.

import numpy as np

N, M = 100, 5000
np.zeros((N, M))
wflynny
  • 18,065
  • 5
  • 46
  • 67
10

To init matrix with M rows and N columns you can use following pattern:

M = 3
N = 2
matrix = [[0] * N for _ in range(M)]
yanefedor
  • 2,132
  • 1
  • 21
  • 37
3

If you want to initialize the matrix with 0s then use the below code

# for m*n matrix
matrix = [[0] * m for i in range(n)]
1
M=[]
n=int(input())
m=int(input())
for j in range(n):
   l=[]
   for k in range(m):
       l.append(0)
   M.append(l)
print(M)

This is the traditional way of doing it matrix[m,n], However, python offers many cool ways of doing so as mentioned in other answers.

-2

i find that this method is the easies method to create a matrix

    rows = 3
    columns = 4
    matrix = [[0] * columns] * rows

my output:


    [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

if you want to print the matrix use this:

    for i in range(rows):
         for j in range(columns):
             print(matrix[i][j], end=' ')
         print()

my output:

   0 0 0 0 
   0 0 0 0 
   0 0 0 0 
  • 3
    Careful, this creates a bunch of aliases: all your rows are the same vector. E.g., `matrix[1][2]=1` then `print(matrix)` will output `[[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]]`, which is certainly not what you want. – joanis Jan 14 '22 at 22:15
  • The other solutions above, some of which use a comprehension to get all the rows, may seem longer, but they avoid this row aliasing problem. – joanis Jan 14 '22 at 22:16
-5
rows = 3
columns = 2
M = [[0]*columns]*rows

Or you could also use '' instead of 0

print(M)

Output:

M = [[0, 0], [0, 0], [0, 0]] 
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 5
    What this code does is creating three references to the same row. So now if you change any value in the Matrix all rows will have the same value. For instance M[0][0] = 10, you will get M = [[10, 0], [10, 0], [10, 0]]. Therefore you need to use list comprehension as provided above. However, I would recommend using numpy for it. – Vlad Bezden Feb 07 '20 at 11:43