how to divide a binary cell into uneven size

.i have 4 binary(0's and 1's ) cells of sizes 1*1120, 1*1344, 1*868, 1*812... now, i need to split or do partition on each cell i.e the entire cell must divide into each 8 bits,so that output of cell's must be of size 1st cell: 8*140, 2nd cell: 8*168, 3rd cell must be 8*109.. 109 because 108 columns contains 108*8=864 binary numbers,and there will remain 4 binary number's.. these 4 binary numbers must store in another column i.e 109th column.. so 3rd cell size must be 8*109.. and 4th cell size must be 8*102 ... and finally i need to calculate decimal value for each splitted file.. in case of 3rd cell, the 109th column contains 4 elements, these 4 elements also must convert into decimal value... final decimal vector must contain size of 1*140, 1*168, 1*109, 1*102....

1 Comment

Please post a samll example of the input data. In the case of the 4 remaining bits: are these the most or least significant bits? What is e,g, the wanted output value for {1,0,1,0}?

Sign in to comment.

Answers (1)

Jan
Jan on 15 Feb 2017
Edited: Jan on 17 Feb 2017
Working with cells is not efficient here. If possible store elements of equal size and type in a numerical vector. But it works with cells also:
% [EDITED 2017-02-17 07:44 UTC]
C = {0,1,0,1,0,1,1,1,1,0,1,0}; % Example data
D = BinCell2Dec(C)
And the function for the conversion:
function Num = BinCell2Dec(C)
n8 = ceil(length(C) / 8);
D = zeros(8, n8);
D(1:numel(C)) = [C{:}]; % Convert cell to numerical array
nLast = mod(length(C), 8);
if nLast % Shift last block to right:
D(:, n8) = [zeros(8 - nLast, 1); D(1:nLast, n8)];
end
Num = [128,64,32,16,8,4,2,1] * D;
end

6 Comments

i have a binary cell of size 1*868 (combination of 1's and 0's)..now i need to divide these 868 binary bits by 8..it is not possible..because 4 bits will remain..since 108*8=864...so,when 868 is divided by 8,output cell size should be 1*109...109 because last column size must contain remaining 4 bits..after that,it has to be converted to decimal number..that means 109 column values must convert into decimal number(including 4 bits which are in 109th column)..
Remaining 4 bits means 865,866,867,868 bits..from 1 to 864 bits are placed in 108 columns..now remaining 4 bits which cannot be splitted by 8 are placed in another column ...
according to your code,the last column is being added with 0's inordere to have 8 bits..but,i dont wont to add 0's to the last remaining 4 bits..the last column must have only 4 bits..and these 4 bits must be converted to decimal value..
@Jyothi: Sorry, I cannot follow you. You want to convert the last 4 bit to a decimal number, but to which decimal number? The appending of zeros is done to allow the conversion to an 8 bit decimal in a simple way. If the result does not match your needs, please explain by an example, what you want. So I ask again: What is the wanted output for e.g. {1,1,1,0,0,0,1,1, 1,0,1,0}?
Perhaps you want:
D = zeros(8, floor(length(C) / 8));
D(1:numel(C)) = [C{1:numel(D)}];
Num = [128,64,32,16,8,4,2,1] * D;
Rest = C(length(D+1):length(C));
But what should happen with this Rest?
According to the e.g. {1,1,1,0,0,0,1,1, 1,0,1,0}?u hve given..i want the output as 227,10...
@Jyothi: See the updated code in my answer:
BinCell2Dec({1,1,1,0,0,0,1,1, 1,0,1,0})
% >> [227, 10]

Sign in to comment.

Asked:

on 15 Feb 2017

Edited:

Jan
on 17 Feb 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!