From: Felipe Uribe on 12 Apr 2010 23:43 I'm programming an optimization algorithm, for this I generate a series of binary numbers by vectors with the following command: x=round(rand(1,10)) x= 0 1 1 0 1 0 0 1 1 1 Then convert it to decimal using the command: y=binvec2dec(x) y= 918 By this command I always get positive integers, but I need to evaluate all kinds of numbers in my cost function... My question is, how do I make when converting the binary vector, get numbers with decimals and negatives numbers?
From: Walter Roberson on 13 Apr 2010 00:22 Felipe Uribe wrote: > I'm programming an optimization algorithm, for this I generate a series > of binary numbers by vectors with the following command: > x=round(rand(1,10)) > x= > 0 1 1 0 1 0 0 1 1 1 > > Then convert it to decimal using the command: > y=binvec2dec(x) > y= > 918 > > By this command I always get positive integers, but I need to evaluate > all kinds of numbers in my cost function... My question is, how do I > make when converting the binary vector, get numbers with decimals and > negatives numbers? WordSize = 32; %or 64 x = rand(1,WordSize) >= 0.5; %binary vector xd = sum(x .^ (WordSize-1:-1:0)); if WordSize == 32 y = typecast(uint32(xd),'single'); else y = typecast(uint64(xd),'double'); end y will end up as a signed single precision floating point number if WordSize is 32, and as a signed double precision floating point number otherwise. Be warned that this procedure can potentially generate a representation for +infinity, for -infinity, or for one of the NaN (Not A Number). Also be warned that not very many of the numbers so generated will be close to 1 -- e.g., only about 1 in 2^12 will be between 1 and 2.
From: James Tursa on 13 Apr 2010 02:42 Walter Roberson <roberson(a)hushmail.com> wrote in message <hq0rhm$nds$1(a)canopus.cc.umanitoba.ca>... > Felipe Uribe wrote: > > I'm programming an optimization algorithm, for this I generate a series > > of binary numbers by vectors with the following command: > > x=round(rand(1,10)) > > x= > > 0 1 1 0 1 0 0 1 1 1 > > > > Then convert it to decimal using the command: > > y=binvec2dec(x) > > y= > > 918 > > > > By this command I always get positive integers, but I need to evaluate > > all kinds of numbers in my cost function... My question is, how do I > > make when converting the binary vector, get numbers with decimals and > > negatives numbers? > > WordSize = 32; %or 64 > x = rand(1,WordSize) >= 0.5; %binary vector At this point x is a bunch of 0's and 1's > xd = sum(x .^ (WordSize-1:-1:0)); So what is the point of raising the 0's and 1's to a power? You're going to get the same 0's and 1's you started with. Looks to me like the max value you are going to get for xd is 32 (or 64 respectively). I think you meant something like sum(x.*(2.^(etc))), at least for the 32 case. But this scheme breaks down for 64-bit floating point case because you will lose precision in the sum (low order bits get lost). > if WordSize == 32 > y = typecast(uint32(xd),'single'); > else > y = typecast(uint64(xd),'double'); > end So I don't think your typecast is going to do what you expect, particularly for the 64-double case. I think we need to get more clarification from OP as to what kind of numbers he wants as a result. What range? Uniformly distributed? Random integers or floating point numbers? etc. James Tursa
From: Walter Roberson on 13 Apr 2010 10:37 James Tursa wrote: > I think > you meant something like sum(x.*(2.^(etc))), at least for the 32 case. > But this scheme breaks down for 64-bit floating point case because you > will lose precision in the sum (low order bits get lost). You are right about both issues. I was posting after my bed-time again :-( Both issues can be fixed by using bitset to create the uint* number instead of using sum() and casting it to uint* afterwards.
|
Pages: 1 Prev: Seemingly Unrelated Regression Estimation in MatLab Next: Getting Video Codecs to work |