问题
I am about to create a function in matlab which will accept multiple modulo and their corresponding remainders then it will determine the least possible value that will fit the given modulo conditions. Major trouble is that I am not allowed to use mod() and rem() built-in function in matlab. Can you help me with this?
回答1:
You can easily create custom my_mod and my_rem functions without using mod and rem, and you can use these as you would use mod and rem.
function modulus = my_mod(X, Y)
if isequal(Y, 0)
modulus = X;
elseif isequal(X, Y)
modulus = 0;
elseif (isequal(abs(X), Inf) || isequal(abs(Y), Inf))
modulus = NaN;
else
modulus = X - floor(X./Y) .* Y;
end
return
function remainder = my_rem(X, Y)
if isequal(Y, 0)
remainder = NaN;
elseif isequal(X, Y)
remainder = 0;
elseif (isequal(abs(X), Inf) || isequal(abs(Y), Inf))
remainder = NaN;
else
remainder = X - fix(X./Y) .* Y;
end
return
来源:https://stackoverflow.com/questions/12544240/least-possible-value-as-described-by-modulo-condition-matlab