Live data from Hacker News

In Defense of Matlab Code

runmat.org

81–90 of 174 posts

Re: In Defense of Matlab Code

#81
post #7

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.

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.

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).

Re: In Defense of Matlab Code

#82

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.

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.

Re: In Defense of Matlab Code

#83
At GKN Aerospace, I was hired predominantly to migrate the team off Matlab and rewrite everything in Python. There was pushback from OEMs who wanted their specs to be in Matlab but eventually everyone folded. Having to need 2 licenses to run on 2 cores was horrible UX IIRC. I'm glad I learnt from that experience.

Re: In Defense of Matlab Code

#84
post #7

Earlier quoted context omitted.

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.

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 funct…

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

Re: In Defense of Matlab Code

#87
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.

Re: In Defense of Matlab Code

#88

> 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 maximi…

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.

Re: In Defense of Matlab Code

#89

Earlier quoted context omitted.

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 funct…

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

This is about how array operations in matlab work. In matlab, you can write things such as

    >> [1 2 3] + 1
    ans = [2 3 4]
In this case, the operation `+ 1` is applied in all columns of the array. In this exact manner, when you add a (1 x m) row and a (n x 1) column vector, you add the column to each row element (or you can view it the other way around). So the result is as if you repeat your (n x 1) column m times horizontally, giving you a (n x m) matrix, do the same for the row vertically n times giving you another (n x m) matrix, and then you add these two matrices. So basically adding a row and a column is essentially a shortcut for repeating adding these two (n x m) matrices (and runs faster than actually creating these matrices). This gives a matrix where each column is the old column plus the row element for that row index. For example

    >> [1 2 3] + [1; 2; 3]
    ans = [2 3 4
           3 4 5
           4 5 6]
A very practical example is, as I mentioned, getting all differences between the elements of a time series by writing `S - S'`. Another example, `(1:6)+(1:6)'` gives you the sums for all possible combinations when rolling 2 6-sided dice.

This does not work only with addition and subtraction, but with dot-product and other functions as well. You can do this across arbitrary dimensions, as long as your input matrices non-unit dimensions do not overlap.

Re: In Defense of Matlab Code

#90
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.

Post reply on HN