Most efficient way to convert from binary to dec without bin2dec or bi2de

조회 수: 9 (최근 30일)
Is there a faster way to acomplish the following? The idea is to have an array of 5 bit inputs and convert each sequence of bits into a decimal.
p = [1 1 0 0 1; 1 0 0 0 1]
x = 2
[m n] = size(p);
y = zeros(m, 1);
for ii = 1:m
y(ii) = polyval(p(ii,:),x)
end
I want to be able to input 300,000 rows and 16 columns and eventually 32 or even 64 columns.

채택된 답변

Walter Roberson
Walter Roberson 2019년 7월 5일
sum(p.*(2.^(size(p,2)-1:-1:0)),2)
  댓글 수: 1
John D'Errico
John D'Errico 2019년 7월 6일
With only the minor caveat that if you have more than 52 bits, it will fail, since the result will be in the form of a double.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by