翻译一段Mathematica 语句到matlab

来源:百度知道 编辑:UC知道 时间:2024/05/07 06:26:01
Negabinary[n_Integer] := Module[
{t = (2/3)(4^Floor[Log[4, Abs[n] + 1] + 2] - 1)},
IntegerDigits[BitXor[n + t, t], 2]
]
这个是将一个十进制数n,转换为-2进制数的Mathematica 程序,请大家帮忙转换成matlab!高分!
具体参见:http://mathworld.wolfram.com/Negabinary.html
希望输出的是一个数,比如我输入:7,输出既为-2进制:11011

待求的十进制数是d,

下面程序给出结果negbin_d:

t = (2/3)*(4^(floor(0.5*log2(abs(d)+1)+2)) -1);

[f,et]=log2(max(t));

[f,etd]=log2(max(t+d));

negbin_d = bitxor(rem(floor(t*pow2(1-max(1,et):0)),2), rem(floor((t+d)*pow2(1-max(1,etd):0)),2))

补充:char(negbin_b+'0')

dec2bin这个函数直接用就行额。(MATLAB中)