I've worked around the problem, but the workaround doesn't make any sense to me. This is the complete text of the M file I'm using to demonstrate the problem (this version takes interminably long to run):
function dslope = fixHeads(dslope, heads)
endb = containers.Map('KeyType','uint32','ValueType','double');
stime = tic;
ltime = tic;
for h=1:length(heads)
if ~endb.isKey(heads(h).id)
ds0 = dslope(heads(h).v(1),heads(h).u(1));
dslope = fixStream(dslope(heads(h).v(1),heads(h).u(1)), dslope, endb);
end
if toc(ltime) > 10
dispProgress(stime, h, length(heads));
ltime = tic;
end
end
end
function [dslope] = fixStream(x, dslope, ~)
dslope(5000,2000) = 0;
end
If I change just the dslope = fixStream... line to
dslope = fixStream(ds0, dslope, endb);
...then the program works fine -- as speedy as expected, 3.5 orders of magnitude faster. Why does adding an intermediate variable to the calling function cause the same statement in a subfunction to run 3.5 orders of magnitude faster? Because Matlab is crazy, as far as I know.