Lecture 26: Differences between Python and Matlab

Below, I have copied our Matlab session with details.

Most important differences in one table.

Concept Python Matlab
Vector xs = [1,2,3]
(or np.array([1,2,3]))
v = [1 2 3]
Matrix list of lists or numpy array of shape (m,n) same as vector, use ; to separate rows
default type of number float or int based on syntax e.g. 1.0 vs 1 default is float (double precision)
accessing element in vector/list xs[i] v(i)
indexing starts from 0 starts from 1. e.g. v(1) = 1 for v = [1 2 3]
last element xs[-1] v(end)
slicing xs = [1,2,3,4], xs[1:3] is [2,3] last element not included v = [1 2 3 4], v(2:4) is [2 3 4]
range list(range(10)) 0:9
steppy range list(range(0,10,2)) 0:2:8
strings "asdf" or 'asdf' only 'asdf'
Price Free and open source Costs money, not open source (this also has some advantages, e.g. certain specialized libraries are better)

Other important points:

Variables are always copied by value (not by reference, so none of the mutable/immutable distinction). E.g.:

v = [1 2 3]
w = v
w(1) = 999

Will change the first element of w but not of v. (unlike in Python where w=v would make w point to the same list as v and any change in w's elements would change the same element in v)

Discussed in demo at the bottom:

  • vectors, matrices etc.
  • series with specified increment
  • accessing index in vector or matrix, slicing. (last element included)
  • size
  • transpose
  • matrix multiplication is built-in *
  • element-wise multiplication .*
  • for, while, if-else formatting
  • eps, realmax, realmin, Inf, pi
  • you can make scripts with .m file format
  • basic plotting (surprise: it's exactly the same)
# for loops
x = ones(1,10);
for n = 2:6
    x(n) = 2 * x(n - 1);
end


# if-else:
if r == c
    A(r,c) = 2;
elseif abs(r-c) == 1
    A(r,c) = -1;
else
    A(r,c) = 0;
end


# Function syntax:
# y is the thing that will be returned

function [y z]= average(x)
if ~isvector(x)
    error('Input must be a vector')
end
y = sum(x)/length(x);
z = 1
end

XKCD Plots

In [1]:
from matplotlib import pyplot as plt
import numpy as np

plt.xkcd()

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.xticks([])
plt.yticks([])
ax.set_ylim([0, 200])
ax.set_xlim([-30, 200])

data = np.concatenate((np.arange(100),100 + 50*np.arange(100)))

plt.annotate(
    'THE DAY I REALIZED\nI CAN MAKE\nXKCD PLOTS',
    xy=(100, 100), arrowprops=dict(arrowstyle='->'), xytext=(120, 50))

plt.plot(data)

plt.xlabel('time')
plt.ylabel('My love for Python')


plt.show()

Our Matlab Session:

Trial>> v = [1 2 3]

v =

     1     2     3

Trial>> v = [1 2 3]'

v =

     1
     2
     3

Trial>> M = [1 2 3; 4 5 6; 7 8 9]

M =

     1     2     3
     4     5     6
     7     8     9

Trial>> M'

ans =

     1     4     7
     2     5     8
     3     6     9

Trial>> M''

ans =

     1     2     3
     4     5     6
     7     8     9

Trial>> M'';
Trial>> M = M'';
Trial>> M = M''

M =

     1     2     3
     4     5     6
     7     8     9

Trial>> M * M

ans =

    30    36    42
    66    81    96
   102   126   150

Trial>> 
Trial>> eye(n)
Undefined function or variable 'n'.

Trial>> eye(3)

ans =

     1     0     0
     0     1     0
     0     0     1

Trial>> M

M =

     1     2     3
     4     5     6
     7     8     9

Trial>> M .* M

ans =

     1     4     9
    16    25    36
    49    64    81

Trial>> M(1,1)

ans =

     1

Trial>> M(2,2)

ans =

     5

Trial>> M

M =

     1     2     3
     4     5     6
     7     8     9

Trial>> M(1:2,1:2)

ans =

     1     2
     4     5

Trial>> M(2:3,2:3)

ans =

     5     6
     8     9

Trial>> 1:10

ans =

     1     2     3     4     5     6     7     8     9    10

Trial>> 1:2:10

ans =

     1     3     5     7     9

Trial>> 1:2:10

ans =

     1     3     5     7     9

Trial>> 1:2:11

ans =

     1     3     5     7     9    11

Trial>> v = 1:15

v =

     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15

Trial>> v(1:10:2)

ans =

     1

Trial>> v(1:2:10)

ans =

     1     3     5     7     9

Trial>> v

v =

     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15

Trial>> v(-1)
Subscript indices must either be real positive integers or logicals.

Trial>> v(end)

ans =

    15

Trial>> M

M =

     1     2     3
     4     5     6
     7     8     9

Trial>> size(M)

ans =

     3     3

Trial>> eps

ans =

   2.2204e-16

Trial>> Inf

ans =

   Inf

Trial>> realmax

ans =

  1.7977e+308

Trial>> realmin

ans =

  2.2251e-308

Trial>> figure
Trial>> v = 1:100

v =

  Columns 1 through 16

     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16

  Columns 17 through 32

    17    18    19    20    21    22    23    24    25    26    27    28    29    30    31    32

  Columns 33 through 48

    33    34    35    36    37    38    39    40    41    42    43    44    45    46    47    48

  Columns 49 through 64

    49    50    51    52    53    54    55    56    57    58    59    60    61    62    63    64

  Columns 65 through 80

    65    66    67    68    69    70    71    72    73    74    75    76    77    78    79    80

  Columns 81 through 96

    81    82    83    84    85    86    87    88    89    90    91    92    93    94    95    96

  Columns 97 through 100

    97    98    99   100

Trial>> v = 0:0.01:1

v =

  Columns 1 through 9

         0    0.0100    0.0200    0.0300    0.0400    0.0500    0.0600    0.0700    0.0800

  Columns 10 through 18

    0.0900    0.1000    0.1100    0.1200    0.1300    0.1400    0.1500    0.1600    0.1700

  Columns 19 through 27

    0.1800    0.1900    0.2000    0.2100    0.2200    0.2300    0.2400    0.2500    0.2600

  Columns 28 through 36

    0.2700    0.2800    0.2900    0.3000    0.3100    0.3200    0.3300    0.3400    0.3500

  Columns 37 through 45

    0.3600    0.3700    0.3800    0.3900    0.4000    0.4100    0.4200    0.4300    0.4400

  Columns 46 through 54

    0.4500    0.4600    0.4700    0.4800    0.4900    0.5000    0.5100    0.5200    0.5300

  Columns 55 through 63

    0.5400    0.5500    0.5600    0.5700    0.5800    0.5900    0.6000    0.6100    0.6200

  Columns 64 through 72

    0.6300    0.6400    0.6500    0.6600    0.6700    0.6800    0.6900    0.7000    0.7100

  Columns 73 through 81

    0.7200    0.7300    0.7400    0.7500    0.7600    0.7700    0.7800    0.7900    0.8000

  Columns 82 through 90

    0.8100    0.8200    0.8300    0.8400    0.8500    0.8600    0.8700    0.8800    0.8900

  Columns 91 through 99

    0.9000    0.9100    0.9200    0.9300    0.9400    0.9500    0.9600    0.9700    0.9800

  Columns 100 through 101

    0.9900    1.0000

Trial>> plot(v .* v)
Trial>> title("awesome plot")
Trial>> random(10)
Error using random (line 64)
The NAME argument must be a distribution name.

Trial>> rand(4)

ans =

    0.1067    0.8173    0.2599    0.1818
    0.9619    0.8687    0.8001    0.2638
    0.0046    0.0844    0.4314    0.1455
    0.7749    0.3998    0.9106    0.1361

Trial>> rand(2,50)

ans =

  Columns 1 through 9

    0.8693    0.5499    0.8530    0.3510    0.4018    0.2399    0.1839    0.4173    0.9027
    0.5797    0.1450    0.6221    0.5132    0.0760    0.1233    0.2400    0.0497    0.9448

  Columns 10 through 18

    0.4909    0.3377    0.3692    0.7803    0.2417    0.0965    0.9421    0.5752    0.2348
    0.4893    0.9001    0.1112    0.3897    0.4039    0.1320    0.9561    0.0598    0.3532

  Columns 19 through 27

    0.8212    0.0430    0.6491    0.6477    0.5470    0.7447    0.6868    0.3685    0.7802
    0.0154    0.1690    0.7317    0.4509    0.2963    0.1890    0.1835    0.6256    0.0811

  Columns 28 through 36

    0.9294    0.4868    0.4468    0.5085    0.8176    0.6443    0.8116    0.3507    0.8759
    0.7757    0.4359    0.3063    0.5108    0.7948    0.3786    0.5328    0.9390    0.5502

  Columns 37 through 45

    0.6225    0.2077    0.4709    0.8443    0.2259    0.2277    0.3111    0.4302    0.9049
    0.5870    0.3012    0.2305    0.1948    0.1707    0.4357    0.9234    0.1848    0.9797

  Columns 46 through 50

    0.4389    0.2581    0.5949    0.6028    0.2217
    0.1111    0.4087    0.2622    0.7112    0.1174

Trial>> A = rand(2, 50)

A =

  Columns 1 through 9

    0.2967    0.4242    0.0855    0.8010    0.9289    0.4886    0.2373    0.9631    0.5211
    0.3188    0.5079    0.2625    0.0292    0.7303    0.5785    0.4588    0.5468    0.2316

  Columns 10 through 18

    0.4889    0.6791    0.3674    0.0377    0.9133    0.0987    0.3354    0.1366    0.1068
    0.6241    0.3955    0.9880    0.8852    0.7962    0.2619    0.6797    0.7212    0.6538

  Columns 19 through 27

    0.4942    0.7150    0.8909    0.6987    0.0305    0.5000    0.9047    0.6177    0.8055
    0.7791    0.9037    0.3342    0.1978    0.7441    0.4799    0.6099    0.8594    0.5767

  Columns 28 through 36

    0.1829    0.8865    0.4899    0.9787    0.5005    0.0596    0.0424    0.5216    0.8181
    0.2399    0.0287    0.1679    0.7127    0.4711    0.6820    0.0714    0.0967    0.8175

  Columns 37 through 45

    0.7224    0.6596    0.9730    0.8003    0.4324    0.0835    0.1734    0.8314    0.0605
    0.1499    0.5186    0.6490    0.4538    0.8253    0.1332    0.3909    0.8034    0.3993

  Columns 46 through 50

    0.5269    0.6569    0.2920    0.0155    0.1672
    0.4168    0.6280    0.4317    0.9841    0.1062

Trial>> scatter(A(1), A(2))
Trial>> scatter(A(:,1), A(:,2))
Trial>> A = rand(100,1)

A =

    0.3724
    0.1981
    0.4897
    0.3395
    0.9516
    0.9203
    0.0527
    0.7379
    0.2691
    0.4228
    0.5479
    0.9427
    0.4177
    0.9831
    0.3015
    0.7011
    0.6663
    0.5391
    0.6981
    0.6665
    0.1781
    0.1280
    0.9991
    0.1711
    0.0326
    0.5612
    0.8819
    0.6692
    0.1904
    0.3689
    0.4607
    0.9816
    0.1564
    0.8555
    0.6448
    0.3763
    0.1909
    0.4283
    0.4820
    0.1206
    0.5895
    0.2262
    0.3846
    0.5830
    0.2518
    0.2904
    0.6171
    0.2653
    0.8244
    0.9827
    0.7302
    0.3439
    0.5841
    0.1078
    0.9063
    0.8797
    0.8178
    0.2607
    0.5944
    0.0225
    0.4253
    0.3127
    0.1615
    0.1788
    0.4229
    0.0942
    0.5985
    0.4709
    0.6959
    0.6999
    0.6385
    0.0336
    0.0688
    0.3196
    0.5309
    0.6544
    0.4076
    0.8200
    0.7184
    0.9686
    0.5313
    0.3251
    0.1056
    0.6110
    0.7788
    0.4235
    0.0908
    0.2665
    0.1537
    0.2810
    0.4401
    0.5271
    0.4574
    0.8754
    0.5181
    0.9436
    0.6377
    0.9577
    0.2407
    0.6761

Trial>> B = rand(100,1)

B =

    0.2891
    0.6718
    0.6951
    0.0680
    0.2548
    0.2240
    0.6678
    0.8444
    0.3445
    0.7805
    0.6753
    0.0067
    0.6022
    0.3868
    0.9160
    0.0012
    0.4624
    0.4243
    0.4609
    0.7702
    0.3225
    0.7847
    0.4714
    0.0358
    0.1759
    0.7218
    0.4735
    0.1527
    0.3411
    0.6074
    0.1917
    0.7384
    0.2428
    0.9174
    0.2691
    0.7655
    0.1887
    0.2875
    0.0911
    0.5762
    0.6834
    0.5466
    0.4257
    0.6444
    0.6476
    0.6790
    0.6358
    0.9452
    0.2089
    0.7093
    0.2362
    0.1194
    0.6073
    0.4501
    0.4587
    0.6619
    0.7703
    0.3502
    0.6620
    0.4162
    0.8419
    0.8329
    0.2564
    0.6135
    0.5822
    0.5407
    0.8699
    0.2648
    0.3181
    0.1192
    0.9398
    0.6456
    0.4795
    0.6393
    0.5447
    0.6473
    0.5439
    0.7210
    0.5225
    0.9937
    0.2187
    0.1058
    0.1097
    0.0636
    0.4046
    0.4484
    0.3658
    0.7635
    0.6279
    0.7720
    0.9329
    0.9727
    0.1920
    0.1389
    0.6963
    0.0938
    0.5254
    0.5303
    0.8611
    0.4849

Trial>> scatter(A, B)