# how to divide a binary cell into uneven size

2 views (last 30 days)

Show older comments

##### 1 Comment

Jan
on 15 Feb 2017

### Answers (1)

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

Jan
on 17 Feb 2017

@Jyothi: See the updated code in my answer:

BinCell2Dec({1,1,1,0,0,0,1,1, 1,0,1,0})

% >> [227, 10]

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!