フィルターのクリア

Error in concatination in binary values

1 回表示 (過去 30 日間)
lilly lord
lilly lord 2022 年 7 月 7 日
コメント済み: Stephen23 2022 年 7 月 8 日
Hi, I am trying to concatinate the two 4-bit binary values to get 8 -bit value and then I have to convert it into decimals. But I am getting few errors. The sample code is given below
c1=[8 14 10 9 6 3 2 7 6 11 6 3 13 15 6 0];
NewR=[];
for i=1:2:length(c1)
n1=de2bi(c1(i),'left-msb');
n2=de2bi(c1(i+1),'left-msb');
result = [n1 n2];
NewR(i)=bi2de(result,'left-msb');
end
NewR
Required output =[ 142 169 99 39 107 99 223 96] % for example the last two digits 6 and 0 when written in binary gives 0110 0000 =96
what I am getting is
NewR =
142 0 169 0 27 0 23 0 107 0 27 0 223 0 12
  1. how to remove zeros in between the values.
  2. Few values are correct but some are wrong.

採用された回答

Stephen23
Stephen23 2022 年 7 月 7 日
V = [8,14,10,9,6,3,2,7,6,11,6,3,13,15,6,0];
N = bin2dec(reshape(dec2bin(V,4).',8,[]).')
N = 8×1
142 169 99 39 107 99 223 96
  1 件のコメント
lilly lord
lilly lord 2022 年 7 月 7 日
Thanks

サインインしてコメントする。

その他の回答 (1 件)

James Tursa
James Tursa 2022 年 7 月 7 日
c1=[8 14 10 9 6 3 2 7 6 11 6 3 13 15 6 0];
NewR = c1(1:2:end)*16 + c1(2:2:end)
NewR = 1×8
142 169 99 39 107 99 223 96
  1 件のコメント
Stephen23
Stephen23 2022 年 7 月 8 日
V = [8,14,10,9,6,3,2,7,6,11,6,3,13,15,6,0];
N = [16,1]*reshape(V,2,[])
N = 1×8
142 169 99 39 107 99 223 96

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAssembly についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by