bad mod function - Matlab

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

bad mod function - Matlab

vicnorton
The mod function in my Mac system is not working.
———————————————————————————————————

Here is essentially the same script run with Matlab.
This is the way the mod function is supposed to work

%% testmod.m  - test the mod function
%    with Matlab '9.3.0.713579 (R2017b)'

fprintf('-- bad mod function? --\n');
for i = 1 : 11
 fprintf('%4d', i);
 if mod(i, 5) == 1; fprintf('\n'); end
end
fprintf('-- good mod function --\n');
for i = 1 : 11
 fprintf('%4d', i);
 if modxy(i, 5) == 1; fprintf('\n'); end
end

function v = modxy(x, y)  % to replace the faulty mod function
 v = x - y .* floor (x ./ y);
end

%{
>> testmod
-- bad mod function? --
  1
  2   3   4   5   6
  7   8   9  10  11
-- good mod function --
  1
  2   3   4   5   6
  7   8   9  10  11
%}