Alternate method for bin2dec function

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 comentarios

Steven Lord
Steven Lord el 28 de Abr. de 2017
What exactly is P1? Describe in words (no code, as little math as you can) what you're trying to do and we may be able to suggest how to solve that problem more quickly than going to and from binary, reshape-ing three times.
Walter Roberson
Walter Roberson el 28 de Abr. de 2017
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 el 28 de Abr. de 2017
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.

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 28 de Abr. de 2017
Editada: Jan el 30 de Abr. de 2017
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 comentarios

Gayathri Vijay
Gayathri Vijay el 28 de Abr. de 2017
Hello
I m not sure why my machine is slow (I3, 2GB RAM, Matlab2010a), Actually the issue is with bin2dec...
tic
img = randi([0, 255], 256, 256);
y=dec2bin(img(:),8);
toc
Elapsed time is 0.057161 seconds.
clear;
clc;
profile on
tic
img = randi([0, 255], 256, 256);
y=dec2bin(img(:),8); %%%% decimal to binary %%%%%%%
P1=reshape(bin2dec((reshape(y, 65536, []))), 256, 256);%%% binary to decimal%%
isequal(img,P1)
toc
Elapsed time is 1.533866 seconds. Self time of bin2dec is 1.478
Walter Roberson
Walter Roberson el 28 de Abr. de 2017
Editada: Walter Roberson el 28 de Abr. de 2017
img = randi([0, 255], 256, 256);
y=dec2bin(img(:),8);
timeit(@() dec2bin(img(:),8), 0)
timeit(@() dec2bin(img(:),8), 0)
timeit(@() bin2dec(y), 0)
timeit(@() bin2dec(y), 0)
ans =
0.006032430392
ans =
0.006053203392
ans =
0.039054237392
ans =
0.040683562392
>> timeit(@() myBin2Dec(y), 0)
ans =
0.000820625392
>> timeit(@() myBin2Dec(y), 0)
ans =
0.000750247587
Note: to use timeit in R2010a, you will need to load it from the File Exchange. It was added to MATLAB itself in R2013b.
Gayathri Vijay
Gayathri Vijay el 29 de Abr. de 2017
Editada: Walter Roberson el 29 de Abr. de 2017
hi...runtime on my machine is comparatively high..Is this issue related with my machine or the version of matlab(R2010a)??? My machine has both windows and ubuntu installed on it, however i m not using both at the same time.. Will my machine slow down if I install multiple operating systems?..
y=dec2bin(img(:),8);
timeit(@() dec2bin(img(:),8), 0)
timeit(@() dec2bin(img(:),8), 0)
timeit(@() bin2dec(y), 0)
timeit(@() bin2dec(y), 0)
ans =
0.0766
ans =
0.0768
ans =
1.3761
ans =
1.4110
To Jan:
Can u give enough inputs about your code?? Eg:the size and data type of s... As i m a beginner its really hard to understand things....thanks Jan
Walter Roberson
Walter Roberson el 29 de Abr. de 2017
The input to Jan's code should be a 2D array of char, with each char being either '0' or '1' . This is exactly the same as bin2dec() expects.
Gayathri Vijay
Gayathri Vijay el 30 de Abr. de 2017
Thanks to Jan and Walter ..
Jan
Jan el 30 de Abr. de 2017
@Gayathri Vijay: My code is a lean version of the original bin2dec, so it expectes exactly the same inputs.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 28 de Abr. de 2017

Editada:

Jan
el 30 de Abr. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by