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;
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!