MATLAB: Using arrayfun on 2d matrix

MATLABmatrixvectorization

for i = 1:D
Xtr = arrayfun(@(x) binarize(x, threshold), Xtrn(:, i));
end
Xtrn is a MxD matrix
Xtr is a MxD matrix
Can we vectorize this loop as well?
This is what binarize does
function X = binarize(X, threshold)
if(X<threshold)
X = 0;
else
X = 1;
end
end

Best Answer

You are overwriting all of Xtr in each iteration of i
You probably just want
Xtr = binarize(Xtrn, threshold)
with
function b = binarize(X, threshold)
b = X >= threshold;