Hi - If matrix A contains only bits,I want a row vector B
(with same length as size(A,2)) that contains the
conversion of each column in A into Decimal.
Example:
A =[ 1 1 0 1 0
0 0 1 1 1
1 1 1 0 1
1 0 0 1 1 ]
B=[bin2dec(A col 1),bin2dec(A col 2),bin2dec(A col 3),...]
"alan dinno" <alan_dinno@hotmail.com> wrote in message
<fjcsag$t98$1@fred.mathworks.com>...
> Hi - If matrix A contains only bits,I want a row vector B
> (with same length as size(A,2)) that contains the
> conversion of each column in A into Decimal.
>
> Example:
>
> A =[ 1 1 0 1 0
> 0 0 1 1 1
> 1 1 1 0 1
> 1 0 0 1 1 ]
>
> B=[bin2dec(A col 1),bin2dec(A col 2),bin2dec(A col 3),...]
>
> Thanks
> alan
"Matt Fig" <spamanon@yahoo.com> wrote in message
<fjcu8k$mcp$1@fred.mathworks.com>...
> "alan dinno" <alan_dinno@hotmail.com> wrote in message
> <fjcsag$t98$1@fred.mathworks.com>...
> > Hi - If matrix A contains only bits,I want a row
vector B
> > (with same length as size(A,2)) that contains the
> > conversion of each column in A into Decimal.
> >
> > Example:
> >
> > A =[ 1 1 0 1 0
> > 0 0 1 1 1
> > 1 1 1 0 1
> > 1 0 0 1 1 ]
> >
> > B=[bin2dec(A col 1),bin2dec(A col 2),bin2dec(A col
3),...]
> >
> > Thanks
> > alan
>
>
> B = bin2dec(num2str(A'))'
Matt - I used it on a big matrix and gave the error:
"??? Error using ==> bin2dec
Binary string must be 52 bits or less."
"alan dinno" <alan_dinno@hotmail.com> wrote in message <fjd062$fjd
$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> <fjcu8k$mcp$1@fred.mathworks.com>...
> > "alan dinno" <alan_dinno@hotmail.com> wrote in message
> > <fjcsag$t98$1@fred.mathworks.com>...
> > > Hi - If matrix A contains only bits,I want a row
> vector B
> > > (with same length as size(A,2)) that contains the
> > > conversion of each column in A into Decimal.
> > >
> > > Example:
> > >
> > > A =[ 1 1 0 1 0
> > > 0 0 1 1 1
> > > 1 1 1 0 1
> > > 1 0 0 1 1 ]
> > >
> > > B=[bin2dec(A col 1),bin2dec(A col 2),bin2dec(A col
> 3),...]
> > >
> > > Thanks
> > > alan
> >
> >
> > B = bin2dec(num2str(A'))'
>
> Matt - I used it on a big matrix and gave the error:
>
> "??? Error using ==> bin2dec
> Binary string must be 52 bits or less."
>
> Thanks
> alan
--------
If vector B is to consist of double precison floating point numbers (double),
these are capable of representing integers to only 53 bits of precision, since
that is the number of bits in their significands. The remaining bits of the 64
are needed for sign and exponent. If the columns of A are longer than that,
your desired task is therefore impossible, in general, if you want exact
representation, and presumably that is why 'bin2dec' complained.
"alan dinno" <alan_dinno@hotmail.com> wrote in message <fjd062$fjd
$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> <fjcu8k$mcp$1@fred.mathworks.com>...
> > "alan dinno" <alan_dinno@hotmail.com> wrote in message
> > <fjcsag$t98$1@fred.mathworks.com>...
> > > Hi - If matrix A contains only bits,I want a row
> vector B
> > > (with same length as size(A,2)) that contains the
> > > conversion of each column in A into Decimal.
> > >
> > > Example:
> > >
> > > A =[ 1 1 0 1 0
> > > 0 0 1 1 1
> > > 1 1 1 0 1
> > > 1 0 0 1 1 ]
> > >
> > > B=[bin2dec(A col 1),bin2dec(A col 2),bin2dec(A col
> 3),...]
> > >
> > > Thanks
> > > alan
> >
> >
> > B = bin2dec(num2str(A'))'
>
> Matt - I used it on a big matrix and gave the error:
>
> "??? Error using ==> bin2dec
> Binary string must be 52 bits or less."
>
> Thanks
> alan
--------
If vector B is to consist of double precison floating point numbers (double),
these are capable of representing integers to only 53 bits of precision, since
that is the number of bits in their significands. The remaining bits of the 64
are needed for sign and exponent. If the columns of A are longer than that,
your desired task is therefore impossible, in general, if you want exact
representation, and presumably that is why 'bin2dec' complained.
"alan dinno" <alan_dinno@hotmail.com> wrote in message <fjd062$fjd
$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> <fjcu8k$mcp$1@fred.mathworks.com>...
> > "alan dinno" <alan_dinno@hotmail.com> wrote in message
> > <fjcsag$t98$1@fred.mathworks.com>...
> > > Hi - If matrix A contains only bits,I want a row
> vector B
> > > (with same length as size(A,2)) that contains the
> > > conversion of each column in A into Decimal.
> > >
> > > Example:
> > >
> > > A =[ 1 1 0 1 0
> > > 0 0 1 1 1
> > > 1 1 1 0 1
> > > 1 0 0 1 1 ]
> > >
> > > B=[bin2dec(A col 1),bin2dec(A col 2),bin2dec(A col
> 3),...]
> > >
> > > Thanks
> > > alan
> >
> >
> > B = bin2dec(num2str(A'))'
>
> Matt - I used it on a big matrix and gave the error:
>
> "??? Error using ==> bin2dec
> Binary string must be 52 bits or less."
>
> Thanks
> alan
--------
If vector B is to consist of double precison floating point numbers (double),
these are capable of representing integers to only 53 bits of precision, since
that is the number of bits in their significands. The remaining bits of the 64
are needed for sign and exponent. If the columns of A are longer than that,
your desired task is therefore impossible, in general, if you want exact
representation, and presumably that is why 'bin2dec' complained.
"alan dinno" <alan_dinno@hotmail.com> wrote in message <fjd062$fjd
$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> <fjcu8k$mcp$1@fred.mathworks.com>...
> > "alan dinno" <alan_dinno@hotmail.com> wrote in message
> > <fjcsag$t98$1@fred.mathworks.com>...
> > > Hi - If matrix A contains only bits,I want a row
> vector B
> > > (with same length as size(A,2)) that contains the
> > > conversion of each column in A into Decimal.
> > >
> > > Example:
> > >
> > > A =[ 1 1 0 1 0
> > > 0 0 1 1 1
> > > 1 1 1 0 1
> > > 1 0 0 1 1 ]
> > >
> > > B=[bin2dec(A col 1),bin2dec(A col 2),bin2dec(A col
> 3),...]
> > >
> > > Thanks
> > > alan
> >
> >
> > B = bin2dec(num2str(A'))'
>
> Matt - I used it on a big matrix and gave the error:
>
> "??? Error using ==> bin2dec
> Binary string must be 52 bits or less."
>
> Thanks
> alan
--------
If vector B is to consist of double precison floating point numbers (double),
these are capable of representing integers to only 53 bits of precision, since
that is the number of bits in their significands. The remaining bits of the 64
are needed for sign and exponent. If the columns of A are longer than that,
your desired task is therefore impossible, in general, if you want exact
representation, and presumably that is why 'bin2dec' complained.
"alan dinno" <alan_dinno@hotmail.com> wrote in message <fjd062$fjd
$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> <fjcu8k$mcp$1@fred.mathworks.com>...
> > "alan dinno" <alan_dinno@hotmail.com> wrote in message
> > <fjcsag$t98$1@fred.mathworks.com>...
> > > Hi - If matrix A contains only bits,I want a row
> vector B
> > > (with same length as size(A,2)) that contains the
> > > conversion of each column in A into Decimal.
> > >
> > > Example:
> > >
> > > A =[ 1 1 0 1 0
> > > 0 0 1 1 1
> > > 1 1 1 0 1
> > > 1 0 0 1 1 ]
> > >
> > > B=[bin2dec(A col 1),bin2dec(A col 2),bin2dec(A col
> 3),...]
> > >
> > > Thanks
> > > alan
> >
> >
> > B = bin2dec(num2str(A'))'
>
> Matt - I used it on a big matrix and gave the error:
>
> "??? Error using ==> bin2dec
> Binary string must be 52 bits or less."
>
> Thanks
> alan
--------
If vector B is to consist of double precison floating point numbers (double),
these are capable of representing integers to only 53 bits of precision, since
that is the number of bits in their significands. The remaining bits of the 64
are needed for sign and exponent. If the columns of A are longer than that,
your desired task is therefore impossible, in general, if you want exact
representation, and presumably that is why 'bin2dec' complained.
"alan dinno" <alan_dinno@hotmail.com> wrote in message <fjd062$fjd
$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> <fjcu8k$mcp$1@fred.mathworks.com>...
> > "alan dinno" <alan_dinno@hotmail.com> wrote in message
> > <fjcsag$t98$1@fred.mathworks.com>...
> > > Hi - If matrix A contains only bits,I want a row
> vector B
> > > (with same length as size(A,2)) that contains the
> > > conversion of each column in A into Decimal.
> > >
> > > Example:
> > >
> > > A =[ 1 1 0 1 0
> > > 0 0 1 1 1
> > > 1 1 1 0 1
> > > 1 0 0 1 1 ]
> > >
> > > B=[bin2dec(A col 1),bin2dec(A col 2),bin2dec(A col
> 3),...]
> > >
> > > Thanks
> > > alan
> >
> >
> > B = bin2dec(num2str(A'))'
>
> Matt - I used it on a big matrix and gave the error:
>
> "??? Error using ==> bin2dec
> Binary string must be 52 bits or less."
>
> Thanks
> alan
--------
If vector B is to consist of double precison floating point numbers (double),
these are capable of representing integers to only 53 bits of precision, since
that is the number of bits in their significands. The remaining bits of the 64
are needed for sign and exponent. If the columns of A are longer than that,
your desired task is therefore impossible, in general, if you want exact
representation, and presumably that is why 'bin2dec' complained.
"alan dinno" <alan_dinno@hotmail.com> wrote in message <fjd062$fjd
$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> <fjcu8k$mcp$1@fred.mathworks.com>...
> > "alan dinno" <alan_dinno@hotmail.com> wrote in message
> > <fjcsag$t98$1@fred.mathworks.com>...
> > > Hi - If matrix A contains only bits,I want a row
> vector B
> > > (with same length as size(A,2)) that contains the
> > > conversion of each column in A into Decimal.
> > >
> > > Example:
> > >
> > > A =[ 1 1 0 1 0
> > > 0 0 1 1 1
> > > 1 1 1 0 1
> > > 1 0 0 1 1 ]
> > >
> > > B=[bin2dec(A col 1),bin2dec(A col 2),bin2dec(A col
> 3),...]
> > >
> > > Thanks
> > > alan
> >
> >
> > B = bin2dec(num2str(A'))'
>
> Matt - I used it on a big matrix and gave the error:
>
> "??? Error using ==> bin2dec
> Binary string must be 52 bits or less."
>
> Thanks
> alan
--------
If vector B is to consist of double precison floating point numbers (double),
these are capable of representing integers to only 53 bits of precision, since
that is the number of bits in their significands. The remaining bits of the 64
are needed for sign and exponent. If the columns of A are longer than that,
your desired task is therefore impossible, in general, if you want exact
representation, and presumably that is why 'bin2dec' complained.
"alan dinno" <alan_dinno@hotmail.com> wrote in message <fjd062$fjd
$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> <fjcu8k$mcp$1@fred.mathworks.com>...
> > "alan dinno" <alan_dinno@hotmail.com> wrote in message
> > <fjcsag$t98$1@fred.mathworks.com>...
> > > Hi - If matrix A contains only bits,I want a row
> vector B
> > > (with same length as size(A,2)) that contains the
> > > conversion of each column in A into Decimal.
> > >
> > > Example:
> > >
> > > A =[ 1 1 0 1 0
> > > 0 0 1 1 1
> > > 1 1 1 0 1
> > > 1 0 0 1 1 ]
> > >
> > > B=[bin2dec(A col 1),bin2dec(A col 2),bin2dec(A col
> 3),...]
> > >
> > > Thanks
> > > alan
> >
> >
> > B = bin2dec(num2str(A'))'
>
> Matt - I used it on a big matrix and gave the error:
>
> "??? Error using ==> bin2dec
> Binary string must be 52 bits or less."
>
> Thanks
> alan
--------
If vector B is to consist of double precison floating point numbers (double),
these are capable of representing integers to only 53 bits of precision, since
that is the number of bits in their significands. The remaining bits of the 64
are needed for sign and exponent. If the columns of A are longer than that,
your desired task is therefore impossible, in general, if you want exact
representation, and presumably that is why 'bin2dec' complained.
Sorry for the eight repetitions of my response. There seems to be a problem
with MathWorks' Newsreader. On my first click on the "Post Message" button it
suffered a Gateway timeout after two or three minutes. On the second click it
finally was entered after over a minute but that resulted in the eight identical
copies. The same thing also happened a few days ago in another thread.
"Roger Stafford"
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <fjd1sg$6nm$1@fred.mathworks.com>...
> "alan dinno" <alan_dinno@hotmail.com> wrote in message
<fjd062$fjd
> $1@fred.mathworks.com>...
> > "Matt Fig" <spamanon@yahoo.com> wrote in message
> > <fjcu8k$mcp$1@fred.mathworks.com>...
> > > "alan dinno" <alan_dinno@hotmail.com> wrote in
message
> > > <fjcsag$t98$1@fred.mathworks.com>...
> > > > Hi - If matrix A contains only bits,I want a row
> > vector B
> > > > (with same length as size(A,2)) that contains the
> > > > conversion of each column in A into Decimal.
> > > >
> > > > Example:
> > > >
> > > > A =[ 1 1 0 1 0
> > > > 0 0 1 1 1
> > > > 1 1 1 0 1
> > > > 1 0 0 1 1 ]
> > > >
> > > > B=[bin2dec(A col 1),bin2dec(A col 2),bin2dec(A
col
> > 3),...]
> > > >
> > > > Thanks
> > > > alan
> > >
> > >
> > > B = bin2dec(num2str(A'))'
> >
> > Matt - I used it on a big matrix and gave the error:
> >
> > "??? Error using ==> bin2dec
> > Binary string must be 52 bits or less."
> >
> > Thanks
> > alan
> --------
> If vector B is to consist of double precison floating
point numbers (double),
> these are capable of representing integers to only 53
bits of precision, since
> that is the number of bits in their significands. The
remaining bits of the 64
> are needed for sign and exponent. If the columns of A
are longer than that,
> your desired task is therefore impossible, in general,
if you want exact
> representation, and presumably that is why 'bin2dec'
complained.
>
> Roger Stafford
>
Will it be possible if vector B contained only integers?
"alan dinno" <alan_dinno@hotmail.com> wrote in message <fjd710$73p
$1@fred.mathworks.com>...
> Will it be possible if vector B contained only integers?
> Thanks
> alan
--------
No, even with "double" integers, starting with the integer, 2^53+1, one
encounters values which cannot be represented exactly by IEEE double
precision floating point numbers. Use format hex and try to add 1 to 2^53.
You will see that the result will still be 2^53. Any integer, which in binary
would span more than 53 bits between its highest and lowest 1-bits, is
impossible to represent in the "double" format. In particular, the integer
2^53+1 in binary would be an impossible 54 bits long with a 1-bit at each
end. There are simply not enough bits available in the 64-bit IEEE format to
accomplish that.
Of course, it would be possible to carry out the desired conversion if you
were willing to accept approximate "double" answers. The following should
accomplish that:
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:fjdfur$npn$1@fred.mathworks.com...
> "alan dinno" <alan_dinno@hotmail.com> wrote in message <fjd710$73p
> $1@fred.mathworks.com>...
>> Will it be possible if vector B contained only integers?
>> Thanks
>> alan
> --------
> No, even with "double" integers, starting with the integer, 2^53+1, one
> encounters values which cannot be represented exactly by IEEE double
> precision floating point numbers. Use format hex and try to add 1 to
> 2^53.
> You will see that the result will still be 2^53. Any integer, which in
> binary
> would span more than 53 bits between its highest and lowest 1-bits, is
> impossible to represent in the "double" format. In particular, the
> integer
> 2^53+1 in binary would be an impossible 54 bits long with a 1-bit at each
> end. There are simply not enough bits available in the 64-bit IEEE format
> to
> accomplish that.
>
> Of course, it would be possible to carry out the desired conversion if
> you
> were willing to accept approximate "double" answers. The following should
> accomplish that:
>
> [m,n] = size(A);
> B=sum(repmat(2.^(m-1:-1:0)',1,n).*A,1);
Another way to write the above is:
A = round(rand(7, 10)); % Sample data
v = ((size(A, 2)-1):-1:0).';
B = A*2.^v
B - bin2dec(num2str(A)) % Should be all zeros
"Steven Lord" <slord@mathworks.com> wrote in message <fji99i$70c
$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
> message news:fjdfur$npn$1@fred.mathworks.com...
> > ........
> > [m,n] = size(A);
> > B=sum(repmat(2.^(m-1:-1:0)',1,n).*A,1);
>
> Another way to write the above is:
>
> A = round(rand(7, 10)); % Sample data
> v = ((size(A, 2)-1):-1:0).';
> B = A*2.^v
> B - bin2dec(num2str(A)) % Should be all zeros
>
> Steve Lord
> slord@mathworks.com
---------
Hello Steve,
It is my understanding that Alan Dinno wants to convert the columns, not
the rows, of binary array A to a row of the corresponding numerical
quantities, in the manner that Matt has suggested using the 'bin2dec'
function.
However, Alan has subsequently stated that he received an error message
from the 'bin2dec' function to the effect that it requires a bit string length of
no more than 52 bits, which would imply that his A arrays possess more than
that number of rows. Based on this, the code I sent him was intended to
overcome this obstacle, though past 53 bits the results necessarily become
subject to possible roundoff error. Has 'bin2dec' been upgraded in more
recent matlab versions than Alan's so as to allow more than 52 bits input?
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
wrote in message <fjjta0$2ev$1@fred.mathworks.com>...
> "Steven Lord" <slord@mathworks.com> wrote in message
<fji99i$70c
> $1@fred.mathworks.com>...
> > "Roger Stafford"
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in
> > message news:fjdfur$npn$1@fred.mathworks.com...
> > > ........
> > > [m,n] = size(A);
> > > B=sum(repmat(2.^(m-1:-1:0)',1,n).*A,1);
> >
> > Another way to write the above is:
> >
> > A = round(rand(7, 10)); % Sample data
> > v = ((size(A, 2)-1):-1:0).';
> > B = A*2.^v
> > B - bin2dec(num2str(A)) % Should be all zeros
> >
> > Steve Lord
> > slord@mathworks.com
> ---------
> Hello Steve,
>
> It is my understanding that Alan Dinno wants to convert
the columns, not
> the rows, of binary array A to a row of the corresponding
numerical
> quantities, in the manner that Matt has suggested using
the 'bin2dec'
> function.
>
> However, Alan has subsequently stated that he received
an error message
> from the 'bin2dec' function to the effect that it requires
a bit string length of
> no more than 52 bits, which would imply that his A arrays
possess more than
> that number of rows. Based on this, the code I sent him
was intended to
> overcome this obstacle, though past 53 bits the results
necessarily become
> subject to possible roundoff error. Has 'bin2dec' been
upgraded in more
> recent matlab versions than Alan's so as to allow more
than 52 bits input?
>
Does he needs to sum of bits from 53th position? May be he
just has to through them out and use hex2dec.
I would like Mathworks to add a similar function hex2int or
extend the hex2dec() with optional flag for setting output
class. Once I wanted to convert hexa string to integer (16,
or 32), hex2dec is no good to scope with overflow and sign,
and it's pretty slow. I ended up doing a small C-mex file
with sscanf(... '%x').
Bruno
Tags for this Thread
Add a New Tag:
Separated by commas
Ex.: root locus, bode
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.
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.