Delete MATLAB #2: Slice Notation

Part 2 in an irregular series about migrating from MATLAB to Python. Slice notation allows you to quickly take a subset of a vector or matrix, by using a short expression in place of the indices. In MATLAB you may be familiar with >> a = [1,2,3,4,5,6,7,8,9,10]; >> a(3:7) ans = 3 4 5 6 … [Read more…]

On digital archaeology

The year is 4018. German is widely studied by scholars of classical antiquity, but all knowledge of the mysterious English language has died out. Scene: A classics department faculty lounge; a few professors are relaxing.  Prof 1 [rushes into the room]: Guys! Guys! You remember that cache of magnetic recordings they found in the ruins … [Read more…]

Delete MATLAB #1: pyplot does LaTeX

My resolution for 2018 (well, one of them) is to delete MATLAB and migrate to Python. Here’s one very good reason: the pyplot package does basically everything you need for plotting, plus you can write axis labels and legends in LaTeX. Here’s some example code (remember this is best done in Anaconda): import matplotlib.pyplot as … [Read more…]

“Intensity” channels

Consider a random variable x taking discrete values $$x \in \{x_1,x_2,\ldots,x_n\},$$ with probability mass function $$p(x_i).$$ If all the possible values of x are positive, it turns out that the following is also a valid probability mass function (all positive, sums to 1): $$q(x_i) = p(x_i) \frac{x_i}{E[X]},$$ where $$E[X] = \sum_i x_i p(x_i)$$ is the expected value of x. … [Read more…]

Knuth shuffle

The Knuth shuffle is a simple, elegant algorithm for randomly permuting a list. It’s not used very often, but it’s one of my favourite algorithms. Here’s how it works: for a list n with elements n[0], n[1], …, n[N-1], pick an index uniformly distributed over 0,1,…,N-1. That index is swapped with n[0]. (If you pick … [Read more…]

Resolution for 2018: Delete MATLAB

Over the past year or so, I’ve been migrating from MATLAB to Anaconda, a free distribution of Python. With the NumPy and PyPlot packages, Anaconda keeps the functionality of plain-vanilla MATLAB while adding the power of the Python language. And if you like graphical IDEs, Anaconda includes Spyder, which replaces the MATLAB graphical environment. Over … [Read more…]

Talking Directly to a Web Server

If you want to talk directly to a web server, you can telnet to port 80 and type your HTTP requests on the console. (You can do this with other services too, if you telnet to the right port.) This might be useful if you’re trying to debug a connection. Here’s an example:

Trace Tricks

The trace of a square matrix is the sum of the elements on the main diagonal. That is, for an n by n square matrix A, the trace of A is $$\mathrm{tr}(A) = \sum_{i=1}^n A_{ii} .$$ This might not seem too exciting at first. However, the trace operator has a neat quasi-commutative property: for matrices U and V, so long as the internal dimensions work out, it … [Read more…]


After a long hiatus, I’ve decided to get back into blogging. I’m closing my old blog and rebooting on this site. For now, this will be a tutorial-style blog. Posts will consist of quick tips, tricks, and lessons on subjects that I (and hopefully you) find useful.