1 dec2bin
将十进制转换为二进制的表现形式
dec2bin(D,N)
- 输入参数为D,将D转换为二进制形式,D必须是整数。例如dec2bin(23)->‘10111’
- 输入参数D、N,将D转换为N位二进制
if N>lenth(D) ,则需要要在前面补零。例如 dec2bin(23,6)->‘010111’
if N<lenth(D) ,输出为D转换为二进制的位数 例如dec2bin(23,4)->‘10111’
2 dec2binvec
将十进制转换为二进制向量
dec2binvec(D,N)
- 输入参数为D,将D转换为二进制形式,D必须是整数。例如dec2bin(23)-> 1 1 1 0 1(逻辑数组)
- 输入参数为D、N,将D转换为N位二进制
if N>lenth(D) ,则需要要在后面补零。例如 dec2bin(23,6)->1 1 1 0 1 0(逻辑数组)
if N<lenth(D) ,输出为,D转换为二进制的位数 例如dec2bin(23,4)->1 1 01 0 1(逻辑数组)
function out = dec2binvec(dec,n)
%DEC2BINVEC Convert decimal number to a binary vector.
%
% DEC2BINVEC(D) returns the binary representation of D as a binary
% vector. The least significant bit is represented by the first
% column. D must be a non-negative integer.
%
% DEC2BINVEC(D,N) produces a binary representation with at least
% N bits.
%
% Example:
% dec2binvec(23) returns [1 1 1 0 1]
%
% See also BINVEC2DEC, DEC2BIN.
%
% MP 11-11-98
% Copyright 1998-2003 The MathWorks, Inc.
% $Revision: 1.5.2.4 $ $Date: 2003/08/29 04:40:56 $
% Error if dec is not defined.
if nargin < 1
error('daq:dec2binvec:argcheck', 'D must be defined. Type ''daqhelp dec2binvec'' for more information.');
end
% Error if D is not a double.
if ~isa(dec, 'double')
error('daq:dec2binvec:argcheck', 'D must be a double.');
end
% Error if a negative number is passed in.
if (dec < 0)
error('daq:dec2binvec:argcheck', 'D must be a positive integer.');
end
% Convert the decimal number to a binary string.
switch nargin
case 1
out = dec2bin(dec);
case 2
out = dec2bin(dec,n);
end
% Convert the binary string, '1011', to a binvec, [1 1 0 1].
out = logical(str2num([fliplr(out);blanks(length(out))]')');