Rhetorical question.
Is anyone providing a useful and innovative service like this for MATLAB?
Surprise me. All the MATLAB users I know are maintaining their own clusters and licensing at great hassle and cost.
Cleaning up after yourself, prologue.
Errors that happen during onCleanup are transformed into warnings? Really?
It doesn’t help that cleanup functions also can’t be closures — they can’t actually respond to data about a resource that was gathered during a program. But first things first.
Hey: if an error happens in my code, that is an error. My program should not continue unless it specifically handles that error. If an error happens while my program is trying to clean up after itself, that means something is wrong and MATLAB should not force my program to blithely continue and wreak further havoc.
I’ve restrained myself from picking too hard on The Mathworks’ decision to promise deterministic destruction for closures and objects, even though it has unacceptable performance penalties. The reason for the restraint is that I can see the argument for object lifecycle management: when your objects correspond to exclusive resources you hold, you do want to have control and guarantees over when they get released.
Well, I just looked into onCleanup and as usual, the Mathworks fucked it up: You cannot write robust programs using onCleanup, because exceptions during cleanup are swallowed.
So I’m going to have to start kicking at the thirty misfeature pileup where MATLAB’s memory management meets its error handling, after all.
There are a number of languages that offer both automatic memory management and exceptions. A few of them are Python, R, Java, and MATLAB. All of these except MATLAB deal with resource cleanup easily with a try/finally statement, which MATLAB lacks, and most also offer some extra sugar in the form of a try-with-resource, which MATLAB tries to do with deterministic destructors, and fails.
One of these things is not like the others:
propagating the original error), it can do so.”
One of these things is not like the others, and the one that fucked up is of course MATLAB.
Profiling
Occasionally I try to work around problems with MATLAB’s shitty slow performance by using its “profiler.”
A profiler is simple in concept: It records how much time a program spends in various functions, so you can tell which ones are slowing you down. There are a couple of different ways to implement a profiler: You could arrange to interrupt the language runtime every millisecond or so and see what functions you’re in the middle of executing. Alternately, you could arrange to record the time every function call begins or ends. Both these techniques involve either instrumenting the runtime, or concurrency and runtime introspection — that’s the kind of runtime stuff that MATLAB doesn’t provide users access to, so instead of being able to actually write a profiler, we have to rely on what profiling ability TMW’s programmers have already built into the runtime.
But fundamentally, that’s the hard part. After you’ve instrumented your runtime and are recording data while the program runs, everything after that is just tabulating and adding up numbers.
Which leads me to ask, how the fuck is MATLAB fucking up at the “adding up numbers” part so badly?
No, I do not have 36 CPUs in my machine, and mainLoop>go does not recurse on itself at all, either.

More after the jump… Read the rest of this entry »
Let’s talk colormaps.

T. Lennert and J. Martinez-Trujillo. Strength of response suppression to distracter stimuli determines
attentional-filtering performance in primate prefrontal neurons. Neuron, 70(1):141–52, Apr 2011. [pubmed]
[for less ranty, try here.]
Dear everyone who uses colormaps: The ‘jet’ colormap, the default colormap in MATLAB, is a perceptual disaster. STOP USING THAT SHIT. I mean, just what is happening in the lower right sector of these plots?
Okay, more background. The scale is a raster of a modulation index (area under ROC, to be particular) for a group of neurons, plotted over the time from stimulus onset. The big salient feature of each plot is a yellow band. You see the yellow band because each raster plot sorts its neurons according to the “latency,” that is, the earliest time that the modulation passed some arbitrary threshold. Because that arbitrary threshold coincides with yellow, the highest-luminance value on the colormap, effectively the authors have chosen a computational and graphical procedure that says “Hey, sort my data that it makes a nice yellow stripe down the middle, NO MATTER WHAT THE DATA ACTUALLY CONTAINS.”
And what is happening to the right of that stripe? Well, because your spatial resolution for chroma differences (and especially for blue) is much worse than that for luminance differences (which is why sane colormaps, that are not “jet”, always have a monotonic luminance component), you have to get your nose right up to the screen to decide that under the stripe is a pretty good mixup of blue and red and yellow — all over the scale. In other words, some of these cells are well modulated past where they pass the arbitrary threshold, but a lot of cells stop being modulated at all after they dinged the threshold. Which is conveniently difficult to discern due to the colormap — and kind of raises the question of how reasonable that threshold setting is, or the cell inclusion criteria are.
We’ll just note in passing that the sorting is done separately for EACH subplot, so that row-by-row comparisons of the cells can’t be done, and the “neuron number” scale on the left is pretty much meaningless. Which also, by the way, fully undermines the claim that modulation for larger ordinal differences(*) happens faster.
I hope you noticed after you got close up to the screen, that the dark red and dark blue values are a lot harder to distinguish than, say, the yellow-to-cyan colors that makes up the middle of the scale. You know, we should be easily able to see when things are at opposite ends of the scale, right? I mean, if we’re plotting our data, I mean.
What kind of insane colormap has the property that values spanning the extreme ends of the scale stand out less, and can’t be distinguished as easily as values in the noisy middle? Why, it’s MATLAB’s default colormap, of course!
You know, ggplot uses a much better preset for its color maps, as well as better alternatives. Just sayin’.
(*) You want to know the ironic punchline? This catastrophe is figure 4 of a paper ABOUT NEURAL PROCESSING OF ORDINAL COLOR SCALES. Seriously!
I’m not sure whether this belongs on the ranting-about-neuroscience blog, or the ranting-about-MATLAB blog. so it goes on both places.
Every time…
Every time I open up MATLAB and try to do something with it, it rewards me with another anachronistic stupidity.
Turns out it is seriously NOT POSSIBLE as of R2009b to have a single figure, containing plots of two images, where each image uses a different colormap.
In the world where you haven’t had to worry about outputting to indexed-color graphics buffers for a decade or so.
Matlab doesn’t know how to draw one ball out of an urn containing one ball.
You are stimulating a discrete Markov process. You have a left-stochastic(*) matrix X where X(j,i) gives the probability of transitioning from state i to the state j. There are a lot of states, and most of the state transition probabilities are zero, so to fit your system into memory, you have built X as a sparse matrix.
You have the index of the current state, i; you want to simulate the next step and randomly draw a value for the index the next state j.
Easy enough to begin with, we find the indices and probabilities of the potential next states like this:
nextStates = find(X(:,i));
weights = X(nextStates,i);
So now we need to generate a random draw from a discrete distribution, where we know the probability of each value of the distribution. Isn’t there a function that does that? Oh yes, randsample. After looking through the help for randsample (a task that is more difficult than it sounds) we might write:
j = randsample(nextStates, 1, 1, full(weights));
Now a really easy question: How and why does this break? Answer below the fold. (hint: look at the category of the post)
(*) while almost all the mathematical literature uses right-stochastic matrices, in MATLAB the sparse matrices are mush slower if you use them that way.
Multiple output arguments
There are many reasons to be told why multiple output arguments in Matlab were a poor design decision. But here is a start. In an unsuccessful attempt to find a code that’s less than O(N*M) in storage for a problem that in any implementation in a real language is O(N) in storage, (*) I saw that someone wants to know,
Is there a similar way to get position information out of a matrix.
I tried something like,
[row,col]=min(min(matrix))
but that returns only the final row that the element is in, not its exact location.
This poor fellow wants to find the indices of a global minimum of a matrix. Now, min by itself finds the column-wise minimum of a matrix, and in a second output returns the row indices. Unless it’s a matrix with a size of 1 in the first dimension, in which case it mysteriously changes behavior and returns the row-wise minimum of a matrix. If the size of the matrix is 1 in both the first and second dimensions, it changes behavior again and gives you the minimum along the third dimension. No external indication is given of which dimension of minimum min happens to be pursuing… but I digress. The questioner just wants a 2-d min!
But here’s the thing: while composing min twice will give the global minimum of a 2-D array, there is no way you can compose two functions in one expression that makes use of the second output. So there is a failure to generalize: [m,i] = min(x) finds a minimum in one dimension, and tells you where the minimum is. The generalization m = min(min(x)) finds a minimum across two dimensions, but there is no way to use this form to find where the minimum is.
But here’s thing: there’s no simple expression that does it. Best I could come up with is:
i = ind2sub(size(x), second_output_of(@min, x(:)))
That will work, leaving second_output_of to be implemented by anyone who perceives a gaping lack in the language at this point — you see, while composing functions with multiple outputs is an ability virtually mandated by the presence of multiple outputs and the mathematical nature of the problems you might want to solve in MATLAB, the ability to use the second output in an expression is just plain missing.
Well, here’s an even more arcane version that uses only built in functions:
i = ind2sub(size(x), find(x == min(x(:)))
This doubles both the time and space requirements as well as bypassing the second output of min() altogether! What a lesson — if you want a simple matlab expression to find the location of a min in 2-d, you can’t use the existing function (the second output of min) that finds the location of a min in 1-D! Instead, you have to hunt down find, ind2sub, and the incantation (:) and live with code that’s twice as slow. There is no simple expression without these incantations that does it; if you want the simple compositionality of applying min twice you are limited to using multiple statements with intermediary temporary variables:
[m,r] = min(x);
[m,c] = min(m);
i = [r(c) c];
While functions with single outputs have the full compositionality of algebra available to them (well, unless you want do something simple like subscripting the output of a function, ahem), second and subsequent outputs are not composable unless you first assign their outputs to intermediary variables. This is unacceptable. A requirement of intermediary variables is evil. There is a longer post planned on this, but you may search for clues why, here and here, before I go live with that.
This is by no means the only problem with multiple outputs in MATLAB. More later.
(*) the answer to my original problem was something in the extra add-on ($$$) Signal Processing Toolbox. To sort points along grid lines. Well, I’m not the one paying for my license, except via externalities and frustration.