【发布时间】:2015-04-15 19:00:48
【问题描述】:
我正在编写一个用户定义的函数来将整数转换为二进制。可以用函数转换的最大数应该是二进制数 16 1 秒。如果输入较大的数字作为 d,该函数应显示错误 信息。使用我的代码,我试图根据余数将数字 0 或 1 添加到我的向量 x 中,然后我想反转我的最终向量以显示二进制数字。这是我所拥有的:
function [b] = bina(d)
% Bina is a function that converts integers to binary
x = [];
y = 2;
in = d/2;
if d >=(2^16 -1)
fprintf('This number is too big')
else
while in > 1
if in >= 1
r = rem(in,y);
x = [x r]
end
end
end
end
【问题讨论】:
-
如何使用
de2bi:mathworks.com/help/comm/ref/de2bi.html -
@m.s.这需要许多人可能没有的通信工具箱。
-
这是作业吗?否则以八度音阶查看
dec2bin。 -
你没有在while循环中更新变量
in,那将是一个无限循环。
标签: matlab