Command to pad zeros to specific locations in binary numbers?

后端 未结 5 1495
孤独总比滥情好
孤独总比滥情好 2021-01-07 02:48

I need to pad zeros to specific locations in binary numbers. Looping the array form of a binary number such as dec2bin(43) and adding the zeros and adjusting th

5条回答
  •  梦毁少年i
    2021-01-07 03:47

    Although not much of a conceptual improvement, the following will do automatic re-indexing and assignment of the old values on a pre-padded matrix:

    >> 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
    

提交回复
热议问题