Skip to content(if available)orjump to list(if available)

In Defense of Matlab Code

In Defense of Matlab Code

92 comments

·December 12, 2025

doawoo

I want to come out and say that a long time ago at a startup we needed to generate a very particular type of analysis graph for a human operator to review in our SaaS.

and I just straight up installed GNU Octave on the server and called out to it from python, using the exact code the mathematician had devised.

fithisux

The most sensible thing I've heard this year.

lemonwaterlime

There's also Julia.

Earlier in my career, I found that my employers would often not buy Matlab licenses, or would make everyone share even when it was a resource needed daily by everyone. Not having access to the closed-source, proprietary tool hurt my ability to be effective. So I started doing my "whiteboard coding" in Julia and still do.

fph

Precisely; today Julia already solves many of those problems.

It also removes many of Matlab's footguns like `[1,2,3] + [4;5;6]`, or also `diag(rand(m,n))` doing two different things depending on whether m or n are 1.

freehorse

Why is the `[1,2,3] + [4;5;6]` syntax a footgun? It is a very concise, comprehensible and easy way to create matrices in many cases. Eg if you have a timeseries S, then `S - S'` gives all the distances/differences between all its elements. Or you have 2 string arrays and you want all combinations between the two.

The diag is admittedly unfortunate and it has confused me myself, it should actually be 2 different functions (which are sort of reverse of each other, weirdly making it sort of an involution).

BobbyTables2

What does it even mean to add a 1x3 matrix to a 3x1 matrix ?

hatmatrix

An understated advantage of Julia over MATLAB is the use of brackets over parentheses for array slicing, which improves readability even further.

The most cogent argument for the use of parentheses for array slicing (which derives from Fortran, another language that I love) is that it can be thought of as a lookup table, but in practice it's useful to immediately identify if you are calling a function or slicing an array.

drnick1

I don't think Julia really solves any problems that aren't already solved by Python. Python is sometimes slower (hot loops), but for that you have Numba. And if something is truly performance critical, it should be written or rewritten in C++ anyway.

But Julia also introduces new problems, such as JIT warmup (so it's not really suitable for scripting) and is still not considered trustworthy:

https://yuri.is/not-julia/

SatvikBeri

> Python is sometimes slower (hot loops), but for that you have Numba

This is a huge understatement. At the hedge fund I work at, I learned Julia by porting a heavily optimized Python pipeline. Hundreds of hours had gone into the Python version – it was essentially entirely glue code over C.

In about two weeks of learning Julia, I ported the pipeline and got it 14x faster. This was worth multiple senior FTE salaries. With the same amount of effort, my coworkers – who are much better engineers than I am – had not managed to get any significant part of the pipeline onto Numba.

> And if something is truly performance critical, it should be written or rewritten in C++ anyway.

Part of our interview process is a take-home where we ask candidates to build the fastest version of a pipeline they possibly can. People usually use C++ or Julia. All of the fastest answers are in Julia.

sundarurfriend

As your comment already hints at, using Python often ends up a hodgepodge of libraries and tools glued together, that work for their limited scope but show their shaky foundations any time your work is outside of those parts. Having worked with researchers and engineers for years on their codebases, there is already too much "throw shit at the wall and see what sticks" temptation in this type of code (because they'd much rather be working on their research than on the code), and the Python way of doing things actively encourages that. Julia's type hierarchies, integrated easy package management, and many elements of its design make writing better code easier and even the smoother path.

> I don't think Julia really solves any problems that aren't already solved by Python.

I don't really need proper furniture, the cardboard boxes and books setup I had previously "solved" the same problems, but I feel less worried about random parts of it suddenly buckling, and it is much more ergonomic in practice too.

MillironX

> I don't think Julia really solves any problems that aren't already solved by Python.

But isn't the whole point of this article that Matlab is more readable than Python (i.e. solves the readability problem)? The Matlab and Julia code for the provided example are equivalent[1]: which means Julia has more readable math than Python.

[1]: Technically, the article's code will not work in Julia because Julia gives semantic meaning to commas in brackets, while Matlab does not. It is perfectly valid to use spaces as separators in Matlab, meaning that the following Julia code is also valid Matlab which is equivalent to the Matlab code block provided in the article.

    X = [ 1 2 3 ];
    Y = [ 1 2 3;
          4 5 6;
          7 8 9 ];
    Z = Y * X';
    W = [ Z Z ];

forgotpwd16

>I don't think Julia really solves any problems that aren't already solved by Python.

You read the article that compares MATLAB to Python? It's saying MATLAB, although some issues exist, still relevant because it's math-like. GP points out Julia is also math-like without those issues.

kelipso

Sometimes slower? No, always slower. And no one wants to deal with the mess that is creating an interface with C or C++. And I wouldn’t want to code in that either, way too much time, effort, headache.

nallana

In Julia, you explicitly need to still reason about and select GPU drivers + manage residency of tensors; in RunMat we abstract that away, and just do it for you. You just write math, and we do an equivalent of a JIT to just figure out when to run it on GPU for you.

Our goal is to make a runtime that lets people stay at the math layer as much as possible, and run the math as fast as possible.

dcanelhas

I remember the pitch for Julia early on being matlab-like syntax, C-like performance. When I've heard Julia mentioned more recently, the main feature that gets highlighted is multiple-dispatch.

https://www.youtube.com/watch?v=kc9HwsxE1OY

I think it seems pretty interesting.

shiroiuma

Julia is actually faster than C for some things.

a-dub

julia is still clunky for these purposes! you can't even plot two things at the same time without it being weird and there's still a ton of textual noise when expressing linear algebra in it. (in fact, i'd argue the type system makes it worse!)

matlab is like what it would look like to put the math in an ascii email just like how python is what it would look like to write pseudocode and in both cases it is a good thing.

constantcrying

Julia competes with the scientific computing aspect of matlab, which is easily the worst part of matlab and the one which the easiest to replace.

Companies do not buy matlab to do scientific computing. They buy matlab, because it is the only software package in the world where you can get basically everything you ever want to do with software from a single vendor.

moregrist

In addition: Simulink, the documentation (which is superb), and support from a field application engineer is essentially a support contract and phone call away.

I say this as someone who’d be quite happy never seeing Matlab code again: Mathworks puts a lot of effort into support and engineering applications.

mNovak

As an engineer, I use Matlab (or rather, Octave the free equivalent) all the time. It's really great for numerical computing and plotting. Most things 'just work', there's a sizeable collection of packages, and I personally like how flexible the function inputs are.

Biggest drawback though is that it's over-optimized for matrix math, that it forces you to think about everything as matrices, even if that's not how your data naturally lies. The first thing they teach about performant Matlab code is that simple for-loops will tank performance. And you feel it pretty quickly, I saw a case once of some image processing, with a 1000x speedup from Matlab-optimized syntax.

Other things issues I've run into are string handling (painful), and generally OOP is unnatural. Would love to see something with the convenient math syntax of Matlab, but with broader ease of use of something like JS.

nallana

@mNovak -- super helpful note! Thank you!

Author of RunMat (this project) here --

> The first thing they teach about performant Matlab code is that simple for-loops will tank performance.

Yes! Since in RunMat we're building a computation graph and fusing operations into GPU kernels, we built the foundations to extend this to loop fusion.

That should allow RunMat to take loops as written, and unwrap the matrix math in the computation graph into singular GPU programs -- effectively letting loop written math run super fast too.

Will share more on this soon as we finish loop fusion, but see `docs/fusion/INTERNAL_NOTE_FLOOPS_VM_OPS.md` in the repo if curious (we're also creating VM ops for math idioms where they're advantageous).

> Would love to see something with the convenient math syntax of Matlab, but with broader ease of use of something like JS.

What does "convenient math syntax of Matlab, but with broader ease of use of something like JS" look like to you? What do you wish you could do with Matlab but can't / it doesn't do well with?

dkarl

Piggybacking on this comment to say, I bet a lot of people's first question will be, why aren't you contributing to Octave instead of starting a new project? After reading this declaration of the RunMat vision, the first thing I did was ctrl-f Octave to make sure I hadn't missed it.

Honest question, Octave is an old project that never gained as much traction as Julia or NumPy, so I'm sure it has problems, and I wouldn't be surprised if you have excellent reasons for starting fresh. I'm just curious to hear what they are, and I suspect you'll save yourself some time fielding the same question over and over if you add a few sentences about it. I did find [1] on the site, and read it, but I'm still not clear on if you considered e.g. adding a JIT to Octave.

[1] https://runmat.org/blog/matlab-alternatives

queuebert

> Biggest drawback though is that it's over-optimized for matrix math ...

I think this is what inspired the creation of Julia -- they wanted a Matlab clone where for loops were fast because some problems don't fit the matrix mindset.

hatmatrix

It's one of those languages that outgrew its original purpose, as did Python IMHO. So non-matrix operations like string processing and manipulation of data structures like tables (surprisingly, graphs are not bad) become unwieldy in MATLAB - much like Python's syntax becomes unwieldy in array calculations, as illustrated in the original post.

ubj

Matlab is an great tool, if you can afford it.

It was a very unpleasant feeling when I graduated from my PhD and realized that most, if not all, of the Matlab scripts I had used for my research would now be useless to me unless I joined a company or national laboratory that paid for licenses with the specific toolboxes I had used.

I'm glad that a significant portion of tools in my current field are in open source languages such as Python and Julia. It widens access to other researchers who can then build upon it.

(And yes, I'm aware of Octave. It does not have the capabilities of Matlab in the areas that I worked in, and was not able to run all of my PhD scripts. I have not tried RunMat yet, but am looking forward to experimenting with it.)

drnick1

> I graduated from my PhD and realized that most, if not all, of the Matlab scripts I had used for my research would now be useless

And this is why you should write free software and, as a scientist, develop algorithms that do not rely on the facilities of a specific language or platform. Nothing is more annoying than reading a scientific paper and finding out that 90% of the "implementation" is calling a third party library treated as a blackbox.

bsder

> And yes, I'm aware of Octave. It does not have the capabilities of Matlab in the areas that I worked in

Was there a specific reason for that? Or was it simply nobody wrote the code?

ubj

Octave has not implemented all of Matlab's functionality. You can see a list of Matlab functions that have not yet been implemented in Octave at the link below. It's a long list.

https://hg.savannah.gnu.org/hgweb/octave/file/tip/scripts/he...

EDIT: If the original link above isn't working, here's a fairly recent archived version:

https://web.archive.org/web/20250123192851/https://hg.savann...

kelipso

You could say that no one wrote that code. But Matlab has serious packages in numerous engineering fields and it’s not anywhere close to easily replicable.

It’s like how open source will never replace Excel but probably worse because it’s multiple fields and it’s way harder to replicate it.

NeroVanbierv

For me, the main appeal to MATLAB was the REPL experience, allowing me to experiment with ideas and inspect results in the UI. Python notebook have bridged the gap a bit, but always require a combination of libraries (with different docs & design choices) to write a single script.

cogman10

Yup, for both Matlab and Maple the big feature was the Jupyter notebook experience right out the box. On top of that, at the time it had a pretty high number of math functions implemented.

esalman

I dislike Matlab's licensing and spaghetti style coding practices like anyone else, but there's another reason python or other modern replacements are scorned at. There are some algorithm/theorem implementations that will produce different results based on the platform and version you are using. With Matlab the chance of that happening is much much lower, if not zero.

themeiguoren

Of the things matlab has going for it, looking just like the math is pretty far down the list. Numpy is a bit more verbose but still 1-to-1 with the whiteboard. The last big pain point was solved (https://peps.python.org/pep-0465/) with the dedicated matmul operator in python 3.5.

Real advantages of matlab:

* Simulink

* Autocoding straight to embedded

* Reproducible & easily versioned environment

* Single-source dependency easier to get security to sign off on

* Plotting still better than anything else

Big disadvantages of matlab:

* Cost

* Lock-in

* Bad namespaces

* Bad typing

* 1-indexing

* Small package ecosystem

* Low interoperability & support in 3rd party toolchains

amluto

> Autocoding straight to embedded

I used this twenty-something years ago. It worked, but I would not have wanted to use it for anything serious. Admittedly, at the time, C on embedded platforms was a truly awful experience, but the C (and Rust, etc) toolchain situation is massively improved these days.

> Plotting still better than anything else

Is it? IIRC one could fairly easily get a plot displayed on a screen, but if you wanted nice vector output suitable for use in a PDF, the experience was not enjoyable.

sevensor

> The issue was never the syntax—it was the runtime. Why readable math still matters in a world aided by LLM-assisted code generation

I’m going to stop you right there. Matlab has 5 issues:

1. The license

2. Most users don’t understand what makes Matlab special and they write for loops over their arrays.

3. The other license

4. The other license

5. The license server

Mathworks seems to have set up licensing to maximize how much revenue they can extract with no thought given to how deeply annoying it is to use.

analog31

For me the friction of dealing with licenses would make it hard to fully integrate a commercial package into my routine. Commercial developers have to decide how they expect a product to be used, so they can allocate finite resources. This invariably imposes limits on users.

In my case, trivial uses are as important as high-visibility projects. I can spin up a complete Python installation to do something like log data from some sensors in the lab, while I do something in another lab, and have something going at my desk, and at home. I use hobby projects to learn new skills. I've played with CircuitPython to create little gadgets that my less technically inclined colleagues can work with. I encouraged my kids to learn Python. I write little apps and give them to colleage. I probably have a dozen Python installations running here and there at any moment.

This isn't a slam on Matlab, since I know it has a loyal following. And I'm unaware of an alternative to Simulink, if that's your bag. And Matlab might be doing the right thing for their business. My impression is that most "engineering software" is geared towards the engineer sitting at a stationary workstation all day, like a CAD operator. And this may be the main way that software is used. Maybe I'm the freak.

finbarr1987

That's precisely the point(s), the runtime's issues (closed source, cost, etc) are what is helping with the declining popularity of the language when really the language can be handy to people who work in math-heavy industries.

thankfully there are fast open source alternatives out there now, hint hint runmat ;)

dwheeler

Many people use Octave https://octave.org/ which is compatible (generally) with Matlab, supports this simple syntax, and is open source software. Indeed, I've taken at least one class where the instructor asked people use Octave for these kinds of calculations.

nallana

Yep -- Octave was very helpful for me in school.

Octave is not particularly fast.

RunMat is very fast (orders of magnitude -- see benchmarks).

ChaitanyaPatel

For MATLAB, there exist many high quality free and/r open source toolboxes from community and academia.

Also there are high quality free and/or open source alternatives.

GNU Octave https://octave.org and Octave online https://octave-online.net/

Freemat https://freemat.sourceforge.net/ (sadly no ongoing development)

Scilab https://www.scilab.org/ and Scilab online https://cloud.scilab.in/

hatmatrix

Indeed, there are many high-quality alternatives (sometimes described as "MATLAB clones" back in the day) that never gained bigger traction.

Among modern alternatives that don't strictly follow MATLAB syntax, Julia has the biggest mindshare now?

GNU Octave, as a superset of the MATLAB language, was (is) most capable of running existing MATLAB code. While Octave implemented some solvers better than MATLAB, the former just could not replicate a large enough portion of the latter's functionality that many scientists/engineers were unable to fully commit to it. I wonder whether runmat.org would run up against this same problem.

The other killer app of MATLAB is Simulink, which to my knowledge is not replicated in any other open source ecosystem.

nallana

Shameless plug for RunMat (we wrote this blog article, also an open source alternative for MATLAB):

https://runmat.org

readme

glad I have your ear

as much as I love the meme in your post, it's the reason I won't be able to share it with work colleagues who use matlab every day

just something to consider

nallana

Appreciate the comment actually! It's good feedback -- we weren't sure if it's mixing work/memes too much, and keeping our materials clean like engineering docs are probably a good way to go. We may edit it out of the post.

Cheers for the comment!

blt

An underrated aspect of Matlab is its call-by-value semantics. Function arguments are copied by default. Python+NumPy is call-by-reference; mutations to array arguments are visible to the caller. This creates a big class of bugs that is hard for non-programmers to understand.

boscillator

Interesting... I wrote a similar post about MATLAB's syntax a while ago, and I still think MATLAB is one of the best calculators on the market.

RunMat is an interesting idea, but a lot of MATLAB's utility comes from the toolboxes, and unless RunMat supports every single toolbox I need, I'm going to be reaching for that expensive MATLAB license over and over again.

nallana

Yep! Makes sense. Though I think the cost of writing these toolboxes is lim --> 0.

Will have a really solid rust inspired package manager soon, and a single #macro to expose a rust function in the RunMat script's namespace (= easy to bring any aspects of the rust ecosystem to RunMat).

ModernMech

It's funny that you listed 1-based index as a strength, and another poster here lists it as a weakness. Goes to show there's really no agreement when it comes to indexing!