MATLAB中 dec2bin 与 dec2binvec的区别

1 dec2bin

将十进制转换为二进制的表现形式

dec2bin(D,N)

  1. 输入参数为D,将D转换为二进制形式,D必须是整数。例如dec2bin(23)->‘10111’
  2. 输入参数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)

  1. 输入参数为D,将D转换为二进制形式,D必须是整数。例如dec2bin(23)-> 1 1 1 0 1(逻辑数组)
  2. 输入参数为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))]')');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山丘之王岳岳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值