Your function TA
can be re-written as
function val = TA(test, ref)
N = size(test,2);
M = size(ref,2);
Y = fix( (1:max(M,N)) * min(M,N)/max(M,N) );
Y(Y == 0) = 1;
if (N>M)
d = abs(test-ref(Y));
else
d = abs(ref-test(Y));
end
val = sum(d);
end
This will be faster, because:
- you didn't pre-allocate variables before the loops; this necessitates re-sizing the variable on each iteration, which is slow
- at least one
if
has been eliminated; branching is relatively slow on most modern CPUs. Where easy to avoid, avoid. - you think bottom-up (a matrix is nothing more than a container of values) rather than top-down (a matrix is the value, it just happens to be compound). MATLAB specializes in the latter; you can make much bigger steps.