Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fast binary to decimal matrix question

Subject: fast binary to decimal matrix question

From: Bluebird

Date: 25 Feb, 2010 17:54:06

Message: 1 of 5

Hello All..

I have a big matrix of binary values and i want to change it to decimal, following is what i am doing and this is very slow for me as i am required to apply the transformation on thousands of matrices


X=ones([10000,3]); % assume one the binary matrix of 10,000 binary number

tic
F=bin2dec(num2str(X,'%-1d'));
toc
      



this the code that i am doing and on average the time it takes is around 0.3-0.4 seconds, this is really slow for me,

Can you please suggest a faster coding..

Many many thanks

Subject: fast binary to decimal matrix question

From: Rune Allnor

Date: 25 Feb, 2010 18:25:21

Message: 2 of 5

On 25 Feb, 18:54, "Bluebird " <wlo...@ryerson.ca> wrote:
> Hello All..
>
> I have a big matrix of binary values and i want to change it to decimal, following is what i am doing and this is very slow for me as i am required to apply the transformation on thousands of matrices
>
> X=ones([10000,3]);  % assume one the binary matrix of 10,000 binary number
>
> tic
> F=bin2dec(num2str(X,'%-1d'));
> toc
>
> this the code that i am doing  and on average the time it takes is around 0.3-0.4 seconds, this is really slow for me,
>
> Can you please suggest a faster coding..

Not with matlab.

This is the kind of thing you would need to do in C or C++
(or even assembler) to gain significant speed.

But then, have the computer run overninght. One hour is
3600 seconds, so you should be able to convert about 5000
matrices per hour.

Rune

Subject: fast binary to decimal matrix question

From: Walter Roberson

Date: 25 Feb, 2010 18:44:09

Message: 3 of 5

Bluebird wrote:

> I have a big matrix of binary values and i want to change it to decimal,
> following is what i am doing and this is very slow for me as i am
> required to apply the transformation on thousands of matrices
>
>
> X=ones([10000,3]); % assume one the binary matrix of 10,000 binary number
>
> tic
> F=bin2dec(num2str(X,'%-1d'));
> toc

F=bin2dec(char(X + '0'));


Though if you only have 3 columns, I would suggest time-testing

F = X * [4 2 1].';

and

F = X(:,1)*4 + X(:,2)*2 + X(:,3);

and there's probably also a sum(bsxfun()) solution that might come out
faster.

Subject: fast binary to decimal matrix question

From: Bluebird

Date: 25 Feb, 2010 19:03:06

Message: 4 of 5

Walter Roberson <roberson@hushmail.com> wrote in message <hm6gdq$ker$1@canopus.cc.umanitoba.ca>...
> Bluebird wrote:
>
> > I have a big matrix of binary values and i want to change it to decimal,
> > following is what i am doing and this is very slow for me as i am
> > required to apply the transformation on thousands of matrices
> >
> >
> > X=ones([10000,3]); % assume one the binary matrix of 10,000 binary number
> >
> > tic
> > F=bin2dec(num2str(X,'%-1d'));
> > toc
>
> F=bin2dec(char(X + '0'));
>
>
> Though if you only have 3 columns, I would suggest time-testing
>
> F = X * [4 2 1].';
>
> and
>
> F = X(:,1)*4 + X(:,2)*2 + X(:,3);
>
> and there's probably also a sum(bsxfun()) solution that might come out
> faster.

Thank you Walter the time dropped from 0.3 to 0.00016 :) thank you so much... Yeah the thing is i want have more than 10 column so i can make look-up-table for the values and just multiply .... Thanks again.....

Subject: fast binary to decimal matrix question

From: Bluebird

Date: 25 Feb, 2010 19:04:08

Message: 5 of 5

Rune Allnor <allnor@tele.ntnu.no> wrote in message <0d07159e-5c6d-4a37-87d2-867fbbd6e685@e7g2000yqf.googlegroups.com>...
> On 25 Feb, 18:54, "Bluebird " <wlo...@ryerson.ca> wrote:
> > Hello All..
> >
> > I have a big matrix of binary values and i want to change it to decimal, following is what i am doing and this is very slow for me as i am required to apply the transformation on thousands of matrices
> >
> > X=ones([10000,3]);  % assume one the binary matrix of 10,000 binary number
> >
> > tic
> > F=bin2dec(num2str(X,'%-1d'));
> > toc
> >
> > this the code that i am doing  and on average the time it takes is around 0.3-0.4 seconds, this is really slow for me,
> >
> > Can you please suggest a faster coding..
>

Thank you Rune
> Not with matlab.
>
> This is the kind of thing you would need to do in C or C++
> (or even assembler) to gain significant speed.
>
> But then, have the computer run overninght. One hour is
> 3600 seconds, so you should be able to convert about 5000
> matrices per hour.
>
> Rune

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us