how to divide a binary cell into uneven size
Mostrar comentarios más antiguos
.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 comentario
Jan
el 15 de Feb. de 2017
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}?
Respuestas (1)
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 comentarios
Jyothi Alugolu
el 15 de Feb. de 2017
Jyothi Alugolu
el 15 de Feb. de 2017
Jyothi Alugolu
el 15 de Feb. de 2017
Jan
el 15 de Feb. de 2017
@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?
Jyothi Alugolu
el 16 de Feb. de 2017
Jan
el 17 de Feb. de 2017
@Jyothi: See the updated code in my answer:
BinCell2Dec({1,1,1,0,0,0,1,1, 1,0,1,0})
% >> [227, 10]
Categorías
Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!