binary to decimal conversion

1 回表示 (過去 30 日間)
anika hossain
anika hossain 2015 年 9 月 16 日
コメント済み: Walter Roberson 2015 年 9 月 17 日
i have an array wr(i,j) that is 256X8 in binary. now i need to convert it to decimal. so after conversion the size should be 16x16. i am using bi2de() function but i am unsuccessful to do it. how can i do it?

採用された回答

Guillaume
Guillaume 2015 年 9 月 17 日
編集済み: Guillaume 2015 年 9 月 17 日
Note that in theory, conversions to string and back are much slower than purely manipulating numbers. With matlab treating numbers as double by default, it may not save you anything but I would consider bypassing the usage of strings and dec2bin altogether. The code would be:
wr = false(256, 8); %predeclare array for better performance
for row = 1:256 %avoid using i and j, they're functions defined by matlab
for col = 1:8
wr(row, col) = round(RU(4, 4, m)) == calc; %no need for if either. just logical assignment
end
end
wr = sum(bsxfun(@times, 2.^(7:-1:0), wr), 2);
wr = reshape(wr, [16 16]); %not sure why you want to do that.
  4 件のコメント
anika hossain
anika hossain 2015 年 9 月 17 日
as i said, i was saving 1/0 as char in wr(row,col), thats why your code did not work for me. now as per your explanation i am saving it as number and your code works perfectly. thanks
Guillaume
Guillaume 2015 年 9 月 17 日
Well, the whole idea is to never go near a char, as string manipulations are typically order of magnitude slower than just numbers.

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

その他の回答 (1 件)

Kirby Fears
Kirby Fears 2015 年 9 月 16 日
編集済み: Kirby Fears 2015 年 9 月 16 日
Hi Anika,
I used the following code to convert a 16x16 matrix to binary (256x8) and back with no problems. Please check that you are calling the bin2dec function correctly and wr(i,j) is a "char" array of 1's and 0's. It would help if you post your code, your binary array, and your error message.
m=225*ones(16);
mb=dec2bin(m);
m2=bin2dec(mb);
  6 件のコメント
anika hossain
anika hossain 2015 年 9 月 17 日
should not the 256x8 char array be converted to 16x16 array after using 'bin2dec()' function? i thought i could do that, if what to do?
Walter Roberson
Walter Roberson 2015 年 9 月 17 日
No, bin2dec converts N x M arrays to column arrays of length N. You need to reshape() the result to whatever size you want.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by