# Matlab to Python – some code examples

Two years back, I was converting a matlab script to python, here are some of the errors that I encountered during the conversion. Found them documented in that converted script, posting them here for wider audience.

```

```

Matlab to Python

1. () to []

```nd=topo(j,i)
X(j,i)=coords(nd,1)
Y(j,i)=coords(nd,2)
```

SyntaxError: can't assign to function call

```nd=topo[j,i]
X[j,i]=coords[nd,1]
Y[j,i]=coords[nd,2]
```

2. 1 to 0

```X(j,i)=coords(nd,1)
Y(j,i)=coords(nd,2)
```

IndexError: index (2) out of range (0<=index<2) in dimension 1

```X[j,i]=coords[nd,0]
Y[j,i]=coords[nd,1]
```

3. zeros to zeros

B=zeros(2,edof)

In Python

```B=np.zeros((2,edof))
```

4. array to array

D=G*[1 0; 0 1];

in python

```D_ps=G* np.array([[1.0,0.0],[0.0,1.0]])
```

5. % to #

Python

```# is comments in python
```

6. For to for

for i=1:nnel
node(i)=nodes(iel,i);
end

python

```for i in range(nnel):
node[i]=nodes[iel,i]
```

7. Matlab find to python find

L1 = find(coordinates(:,2)==min(coordinates(:,2)))

in python

```l1 = np.where(coords[:,1]==np.min(coords[:,1])
```