Least Possible Value as described by Modulo Condition MATLAB

社会主义新天地 提交于 2020-01-25 07:48:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!