用二进制数填充零到特定位置的命令?

时间:2013-12-26 20:48:37

标签: matlab bit-manipulation

我需要将二进制数填充到特定位置。循环二进制数的数组形式,例如dec2bin(43)并添加零并调整大小,这些都会重新发明轮子。

如何在Matlab中有效地将零填充为二进制数?

循环

positions=[1,3,6];
x=de2bi(43);
xx=flip(x);    

kk=1;
for ii=1:length(x)+length(positions)
    if isequal(positions(kk),xx(ii))
         %Transfer the tail from ii by one index ahead,
         %left out here because I think my method is bad!
         x(ii)=0;
         positions=positions+1;  %Increase positions by one now due to one padding
         kk=kk+1;
    end
end

感觉太多重新发明:基本上如何从Yxx之前获得0,从xxxxYabcd到xxxx0Yabcd,其中xxxxYabcd是二进制数?

示例

  

输入

     
    

101011,(1,3,6)

  
     

输出

     
    

100100110

         

1x010x11x(显示零的位置,其中x插入了零)

  

5 个答案:

答案 0 :(得分:1)

虽然没有太大的概念改进,但以下内容将在预填充矩阵上自动重新索引和分配旧值:

>> xx
xx =
     1     0     1     0     1     1

nPads = length(positions);
nPadsShifts = 1:nPads;           
y = ones(1, length(xx) + nPads); % re-indexing on the new array
y(positions + nPadsShifts) = 0;  % padding values
y(y==1) = xx;                    % set original bit values


>> y
y =
     1     0     0     1     0     0     1     1     0

答案 1 :(得分:1)

您只需使用索引即可完成此操作。要插入扩展向量中的零的位置为insertzeros+(1:length(insertzeros))(示例中insertzeros[1 3 6]):

input = [1 0 1 0 1 1]; %// example data
insertzeros = [1 3 6]; %// example data

output = ones(1,length(input)+length(insertzeros));
output(insertzeros+(1:length(insertzeros))) = 0;
output(output==1) = input;

答案 2 :(得分:1)

比赛开始了! Mendo获胜,gevang是第二名,thewaywewalk是第三名,jaheruddin名列第四名。 Shai的bitshifting是在shai函数中实现的,遗憾的是还没有让它运行得更快。

<强>结果

gevang :2.2e-05
thewaywewalk :5.6975e-05
mendo :2.2102e-05
jaheruddin :0.0001693
shai (poor hhh-implementation) 5.3288e-04

对答案进行热身测试

function test_padding_zeros()

    function myOutput=gevang(xx,positions)
        nPads = length(positions);
        nPadsShifts = 1:nPads;
        myOutput = ones(1, length(xx) + nPads); % re-indexing on the new array
        myOutput(positions + nPadsShifts) = 0;  % padding values
        myOutput(myOutput==1) = xx;                    % set original bit values
    end

    function myOutput=thewaywewalk(x,positions)
        idx = numel(x):-1:1;
        myOutput = num2cell(x);
        myOutput(2,idx(positions)) = {0};
        myOutput = [myOutput{:}];
    end

    function myOutput=jaheruddin(myInput,positions) % myInput can be a row vector or a matrix!
        n = size(myInput,2)+numel(positions);
        myOutput = false(size(myInput,1),n);
        myOutput(:,setxor((1:length(positions)),1:n))=myInput;
    end

    function myOutput=mendo(myInput,positions)
        myOutput = ones(1,length(myInput)+length(positions));
        myOutput(positions+(1:length(positions))) = 0;
        myOutput(myOutput==1) = myInput;
    end

    function out = bizarreBitShift( bNum, fromBit, shiftAmount )
        % construct a mask
        msk = uint32( (2^( fromBit - 1 ) )-1 ); 
        shiftPart = bitand( uint32(bNum), bitcmp(msk) ); % bitcmp - complement of bits
        staticPart = bitand( uint32(bNum), msk );
        out = bitshift( shiftPart , shiftAmount );
        out = bitor( out, staticPart );
    end

    function myOutput=shai(myInput,positions)
        shiftAmount=1;
        myOutput=sprintf('%d',myInput);
        myOutput=bin2dec(myOutput);
        k=0;
        for ii=1:length(positions)
            fromBit=positions(ii)+k;
            myOutput=bizarreBitShift(myOutput, fromBit, shiftAmount);
            k=k+1;
        end

        myOutput=ismember(dec2bin(myOutput),'1');
    end




positions = [1 3 6]; %// example data
myInput = [1 0 1 0 1 1]; %// example data

ggevang=@() gevang(myInput,positions);
tthewaywewalk=@() thewaywewalk(myInput,positions);
mmendo=@() mendo(myInput,positions);
jjaheruddin=@() jaheruddin(myInput,positions);
sshai=@() shai(myInput,positions);

timeit(ggevang)
timeit(tthewaywewalk)
timeit(mmendo)
timeit(jjaheruddin)
timeit(sshai)

end

答案 3 :(得分:0)

如果您正在寻找效率,尝试各种选择永远不会受到伤害。这是另一种方法:

M = magic(5)>3; % Example matrix
positions = [2 3 6]; % Desired padding columns in resulting matrix

n = size(M,2)+numel(positions);
R = false(size(M,1),n);

R(:,setxor(positions,1:n))=M

答案 4 :(得分:0)

Shai为单个二进制数提供了here位移解决方案。我在下面使用它填充零,解决方案可能会更快 - 目前是最慢的解决方案之一(一些不必要的dec2bin)或与Jaheruddin的解决方案大致相同的速度。

function out = bizarreBitShift( bNum, fromBit, shiftAmount )
    % construct a mask
    msk = uint32( (2^( fromBit - 1 ) )-1 ); 
    shiftPart = bitand( uint32(bNum), bitcmp(msk) ); % bitcmp - complement of bits
    staticPart = bitand( uint32(bNum), msk );
    out = bitshift( shiftPart , shiftAmount );
    out = bitor( out, staticPart );
end

function myOutput=shai(myInput,positions)
    shiftAmount=1;
    myOutput=sprintf('%d',myInput);
    myOutput=bin2dec(myOutput);
    k=0;
    for ii=1:length(positions)
        fromBit=positions(ii)+k;
        myOutput=bizarreBitShift(myOutput, fromBit, shiftAmount);
        k=k+1;
    end

    myOutput=ismember(dec2bin(myOutput),'1');
end

positions = [1 3 6]; %// example data
myInput = [1 0 1 0 1 1]; %// example data

sshai=@() shai(myInput,positions);
timeit(sshai)

<强>时序

ans =

   5.3288e-04