MATLAB: Speeding up the for loop

for looploopsspeeding up the program

%My challenge is to find the number of divisors for a number without using "divisors" inbuild function
%this program will work but it's taking too much time for big numbers
%I read about vectorization to reduce the time but found stucked
%help!!!
function y=divisors1(N)
sum=0;
for i=1:floor(N/2)
if lcm(N,i)==N
sum=sum+1;
end
end
y=1+sum;

Best Answer

function y=divisors2(N)
f = factor(N);
[~,~,J] = unique(f);
n = accumarray(J,1);
y = prod(n+1);
end
Test
>> N=27022021
N =
27022021
>> divisors2(N)
ans =
8