How to reduce the time?

Hi. I am trying to convert a decimal matrix in to binary form by using following code:
k = 1;
P = ''; %%P is the output matrix
for i = 1 : numel(s) %%s is the input matrix
P(k,1:bin_total) = bin(fi(s(i),1,8,6));
%%Decimal to binary conversion
k = k+1;
end
I am trying to reduce the time because the input matrix may have 1000 elements. So it's taking very long time for converting in to binary.
Can anyone please give me any idea how to make the code more faster?
Thanks.

댓글 수: 7

Walter Roberson
Walter Roberson 2012년 3월 1일
Is there a requirement to use the fixed point toolbox?
What kind of inputs are in s() ?
8,6 ... you are wanting binary equivalent of fractions?
Shifat
Shifat 2012년 3월 1일
Thank you for your reply.
I find it easiest for decimal to binary conversion by using the fixed point toolbox.
s() is a decimal input matrix where each element may be negative and fractional. For example:
s = [0.1239 -1.9872; -2.1549 -1];
Yes, 8 means the total binary number and 6 means the fractional part only.
Jan
Jan 2012년 3월 7일
Double post, see http://www.mathworks.com/matlabcentral/answers/31130-how-to-reduce-the-time
Walter Roberson
Walter Roberson 2012년 3월 7일
"I find it easiest" is not really an answer to "Is there a requirement to use the fixed point toolbox?"
I do not have the fixed-point toolbox, so I would be unable to test or time solutions based on the fixed-point toolbox. If you *must* use the fixed-point toolbox, I do not have the resources to assist in this matter. If you just _prefer_ the fixed point toolbox then I may be able to come up with something.
Shifat
Shifat 2012년 3월 7일
Actually fixed point tool box is not the requirement.
But i need specific bit system (for example: 2 digits for integer part and 6 digits for fractional part.)
Walter Roberson
Walter Roberson 2012년 3월 7일
Okay -- signed or unsigned?
Shifat
Shifat 2012년 3월 7일
Signed.

댓글을 달려면 로그인하십시오.

답변 (2개)

Laurens Bakker
Laurens Bakker 2012년 3월 7일

0 개 추천

Hi Shifat,
the base MATLAB distribution has a function for this:
P = dec2bin( fix(s) );
where fix() cuts off the fractional part. Let me know if this is not fast enough for you. There are some faster ways of doing it, but that gets somewhat involved.
Cheers,
Laurens

댓글 수: 1

Shifat
Shifat 2012년 3월 7일
Thank you for your help.
But i need the fractional part too and the also need the bit system be fixed (Like 6 bits for fractional part and 2 bits for integer part.)
I found some thing which seems to me more faster than my previous 'for loop'code (which i posted above):
P = bin(fi(decimal_input_matrix,1, 8, 6)) %8 represents whole binary number and 6 represents the length of fractional part
I found that this code takes almost less than half time if i don't use loop.
If you have anything which works more faster than this, please let me know. Thanks.

댓글을 달려면 로그인하십시오.

Shifat
Shifat 2012년 3월 10일

0 개 추천

I am deleting my other post which contains the same subject. Jan Simon answered in that question which helped me a lot. Following answer was given by him:
Pre-allocation!
k = 1;
P = repmat(' ', numel(s), bin_total);
for i = 1 : numel(s) %%s is the input matrix
P(k, :) = bin(fi(s(i),1,8,6));
k = k+1;
end
[EDITED] See these FEX submissions for other decimal to binary conversions:
FEX: floating-number-conversion-to-binary
FEX: conversion-of-fractions
FEX: efficient-convertors-between-binary-and-decimal

댓글 수: 1

Oleg Komarov
Oleg Komarov 2012년 3월 11일
Do not delete posts that have been answered.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2012년 3월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by