Using MRES and PRES as a Dictionary Between MATLAB and Python

Now that both new editions have been published, the MRES and PRES books can be used as a MATLAB-Python and Python-MATLAB dictionary.

I wrote the two new editions of the books MATLAB Recipes for Earth Sciences, 6th Edition and Python Recipes for Earth Sciences, 2nd Edition at the same time. Therefore, the text is largely identical and the code is very similar. Of course, the MATLAB and Python code is different, but since Python is very similar to MATLAB, the codes and their explanations are very similar.

Of course, there are fundamental differences in the syntax, which I described in an older post. These include the indexing of arrays, which is very unusual for MATLAB users, starting with 0 instead of 1, among others. This fundamental difference becomes important when loading and analyzing satellite images, for example. In order to create an RGB composite of Bands 29, 23, and 16 in MATLAB, we can extract the bands from the radiance value data in HYPR by typing

HYP1 = HYPR(:,:,29);
HYP2 = HYPR(:,:,23);
HYP3 = HYPR(:,:,16);

In Python, however, you should type

HYP1 = HYPR[:,:,28]
HYP2 = HYPR[:,:,22]
HYP3 = HYPR[:,:,15]

This is only one example showing how error-prone Python code is because indexing begins with zero and that we must therefore use band–1 as an index.

There are also some things, mostly applications that depend on very specialized functions or apps that are only available for MATLAB, which do not appear in the Python version of the book. However, I have now moved these to the end of the chapter so that the chapters end earlier and there are no gaps within the chapters. Therefore, the Python book with 491 pages is a bit thinner than the MATLAB book with 567 pages.

An example of how to read the two books in parallel is 5.4 Schuster’s Periodogram Method. The periodogram function in SciPy is almost identical to the corresponding function from the Signal Processing Toolbox for MATLAB, although the Python version does not have some of the features of the MATLAB version. As in the rest of the book, however, the periodogram is first computed without using this function, only using standard functions such as the FFT. Again, it becomes clear that the similarities between the two languages predominate when using packages such as NumPy, SciPy, and Matplotlib.

References

Trauth, M.H. (2025) MATLAB Recipes for Earth Sciences – Sixth Edition. Springer International Publishing, 567 p, https://doi.org/10.1007/978-3-031-57949-3.

Trauth, M.H. (2024) Python Recipes for Earth Sciences – Second Edition. Springer International Publishing, 491 p., https://doi.org/10.1007/978-3-031-56906-7.