two minor changes are needed:
function x = fact(n)
x = ones(size(n));
idx = (n>1);
x(idx) = n(idx).*fact(n(idx)-1);
First, you need to initialize x as an array of the same size as n (because for fact(n-1) otherwise the array could be smaller), giving the error you observed.
Second, protect the recursive call to fact(n-1) by doing this only if there is anything to do...