Alternate method for bin2dec function

1 回表示 (過去 30 日間)
Gayathri Vijay
Gayathri Vijay 2017 年 4 月 28 日
編集済み: Jan 2017 年 4 月 30 日
i m looking for an alternate method for bin2dec conversion to speed up my code. bin2dec functions takes 2.5 to 3 sec as run time.Is there any solution. Here is my code:
tic
img = randi([0, 255], 256, 256);
y=dec2bin(img(:),8);
I=reshape(y,256,2048);
P1=reshape(bin2dec(num2str(reshape(I, 65536, []))), 256, 256);
toc
Elapsed time is 1.494633 seconds.
selftime of bin2dec-----1.420 s
Thanks in advance
  3 件のコメント
Walter Roberson
Walter Roberson 2017 年 4 月 28 日
The output of dec2bin is a character array. You reshape that a couple of times (why not just once?), and then you num2str() what is already string, which is a step that will leave things unchanged. Then you bin2dec() that.
Are we to understand from this that there is a hidden transmission over serial line and reception with fscanf('%d') between the creation of y and P1 ?
Gayathri Vijay
Gayathri Vijay 2017 年 4 月 28 日
i m working on a bit level encryption of an image, for which the image is transformed into a binary sequences. After processing, the binary matrix is reconstructed to get the decimal matrix. During the whole process, bin2dec is the time consuming operation which has to be replaced. Is there anyway to speed things up to be under 1 sec or even better, as every msec counts for my application.

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

採用された回答

Jan
Jan 2017 年 4 月 28 日
編集済み: Jan 2017 年 4 月 30 日
You can create a lean version of dec2bin:
function x = myBin2Dec(s)
% Lean version of Matlab's BIN2DEC, see: help bin2dec
n = size(s, 2);
v = s - '0';
twos = pow2(n-1:-1:0);
x = v * twos.';
This reduces the runtime from 0.102 to 0.048 seconds and dec2bin is the bottleneck - you know how to create a lean version now.
I cannot imagine how you get 1.5 seconds. My machine is an old 2.3GHz Core2Duo. Perhaps you have a clear all before and the time is required to reload the files from the disk?
Similar to Steven's question: What is the purpose of the code? I assume that there is a faster version without the conversion to the binary strings.
  6 件のコメント
Gayathri Vijay
Gayathri Vijay 2017 年 4 月 30 日
Thanks to Jan and Walter ..
Jan
Jan 2017 年 4 月 30 日
@Gayathri Vijay: My code is a lean version of the original bin2dec, so it expectes exactly the same inputs.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by