Command to pad zeros to specific locations in binary numbers?

后端 未结 5 1506
孤独总比滥情好
孤独总比滥情好 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条回答
  •  醉酒成梦
    2021-01-07 03:32

    You can do it using just indexing. The positions of the zeros to be inserted in the extended vector are insertzeros+(1:length(insertzeros)) (where insertzeros is [1 3 6] in your example):

    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;
    

提交回复
热议问题