필터 지우기
필터 지우기

How to convert binary elements of a cell into decimal number

조회 수: 7 (최근 30일)
Jyothi Alugolu
Jyothi Alugolu 2017년 2월 8일
댓글: Jyothi Alugolu 2017년 2월 15일
Hello, I have a cell of binary elements with 0's and 1's of size 1*1624..now i have to convert every 8 binary elements from 1624 elements into a decimal number and store in an array..so,the final output must be of size 1*203(each element in 203 must consists of the value of each 8 binary elements...can anyone help me please

채택된 답변

Jan
Jan 2017년 2월 8일
In = {0,1,1,1,0,0,1,0, 1,0,1,1,0,1,0,1}; % 1 x 16
M = reshape([In{:}], 8, []);
Out = [128, 64, 32, 16, 8, 4, 2, 1] * M;
  댓글 수: 2
Guillaume
Guillaume 2017년 2월 8일
Ah, yes, even simpler with matrix multiplication.
Jyothi Alugolu
Jyothi Alugolu 2017년 2월 15일
one more question...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 first cell:8*140, second cell: 8*168, third 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 third cell size must be 8*109..and fourth 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....

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Thibaut Jacqmin
Thibaut Jacqmin 2017년 2월 8일
편집: Thibaut Jacqmin 2017년 2월 8일
Here I create a cell of size 16 (and not 1624) as an example
a = {1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1};
Reshape the cell in a 8xN cell (N = 203 in your case)
a = reshape(a, [8, length(a)/8]);
Convert each column of 8 binary in a decimal number and store it in res
res = [];
for i = 1:size(a, 2)
res(end+1) = bi2de([a{:, i}]);
end
  댓글 수: 5
Thibaut Jacqmin
Thibaut Jacqmin 2017년 2월 8일
You don't have to write 203 in the for loop. Indeed size(a, 2) already equals to 203 after reshaping.
Jyothi Alugolu
Jyothi Alugolu 2017년 2월 10일
if suppose, if the binary text file with 0's and 1's is of size 1*1624.now i need to reshape this 1624 binary elements into 16 *N (N=1624),then i will have 16*101 cell and 8 bits will be remaining..these 8 bits must store in another cell..so,finally the output must be 16*102..since 101 columns will have each 16 bit values and 102th column must have remaining 8 bits..now i need to convert these 102 column values into decimal values...the final output must be a row vector of size 1*102..how to do this?

댓글을 달려면 로그인하십시오.


Alexandra Harkai
Alexandra Harkai 2017년 2월 8일
Considering it's a 1*1624 numeric array you have in the cell A, you can utilise bin2dec to do it after converting the numeric values to a string representation with num2str:
{bin2dec(num2str(reshape(A{:}, 8, size(A{:},2)/8)'))}'

Guillaume
Guillaume 2017년 2월 8일
편집: Guillaume 2017년 2월 8일
Question: Why is the data in a cell array when a normal matrix would work just as well and make the code simpler? That is instead of:
binaryvector = {1, 0, 0, 1, 0, 1, 1, ...};
Have
binaryvector = [1, 0, 0, 1, 0, 1, 1, ...];
You also haven't told us which bit (1st or 8th) is the LSB and MSB.
Anyway, another way, probably the fastest, to convert your array:
binaryvector = num2cell(randi([0 1], 1, 1624)); %create a cell array of random bits for demonstration only. Use your own data
binaryvector = cell2mat(binaryvector); %convert cell array into matrix as cell array is pointless and just makes manipulating the bits harder
%in version R2016b ONLY:
decimalvector = sum(2.^(7:-1:0)' .* reshape(binaryvector, 8, []));
%in version prior to R2016b:
decimalvector = sum(bsxfun(@times, 2.^(7:-1:0)', reshape(binaryvector, 8, [])));
The above assumes the LSB is the 8th bit, if it's the first bit replace the 7:-1:0 by 0:7
  댓글 수: 1
Jan
Jan 2017년 2월 8일
You can omit the sum(), when you use a matrix multiplication.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by