简体   繁体   中英

Matlab: binary valued random variable

Problem 1: I have the decimal representation of a rational. This is the code for generating binary number.

 x(1) = rand();
   [num, den] = rat(x);


          q = 2^32;
          x1 = num / den * q;
          b = dec2bin(x1, bits);
          s = str2num(b')';

UPDATE: The information about Dyadic map expressed in code as

y = mod(x*2, 1)

says that if the input, x is a binary iterate s , then the output should be binary with the bits shifted to the left by one position. But, if I give the input x = 0.1101 or x = 1101 or x= 1 (bit) still the output y is not binary. The machine understands the input as a decimal and hence returns a decimal base number. How can I use this map to model / represent binary valued random variables?

Problem 2: (SOLVED BASED ON THE ANSWER)

Secondly, I need to do another operation involving the command

(X(:,i)>=threshold)*(X(:,i)>=threshold)';

where X is a matrix of real valued numbers and the variable

threshold = 0.5

and i is the index for the element. I keep getting this error

Error using  * 
Both logical inputs must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead.

I tried using the .* but still I keep getting this error. How do I solve these 2 problems?

It shall be helpful if a code is provided.

Problem 1: (UPDATED) This reflects your updates that your goal is a Dyadic Mapping.

Think of Matlab as an environment to abstract the notion of binary numbers. It doesn't have built-in support numerical operations with binary numbers. In fact, it doesn't have a numerical representation of bits. It has strings for bits only. You can put a decimal number through a custom function to make it look binary, but to Matlab its still a float. If you put x = 0.1101 through y= mod(2*x,1) it will treat x as a floating point

Problem 2:

I'm not sure what you're trying to do here. The error is caused by trying to matrix multiply a vector of type logical . Matrix multiplication is only defined for numeric types. A temporary hack would be to add 0.0 to the vectors before multiplying thus casting the values to a double

((X(:,i)>=threshold)+0.0)*((X(:,i)>=threshold)+0.0)';

Problem 1: I have the decimal representation of a rational.

Great. So far so good...

This is the code for generating binary number.

No, this is the code for generating the binary representation of a number. It's the same number that you represented in decimal. I know you think I'm being pedantic, but as far as I can determine, this is the source of your confusion. A number is a number regardless of the representation. Five sheep is five sheep whether you write it in binary, decimal, octal or using the fingers on Hammish's left hand (he's only got 4 left).

Let's change your code slightly.

bits = 32;
r = rand();
[num, den] = rat(r);
q = 2^bits;

x(1) = num / den;

The value stored in x(1) is a rational number. If we type disp(x(1)) in Matlab, it will show us the value of that number in decimal representation . We can change that representation to binary using the dec2bin command:

b(1,:) = dec2bin(round(x(1)*q), bits);

But it's still the same number. (Actually, it's not the same number because we've now limited the precision to bits bits instead of the native 53 bits Matlab generated it with. More on this later.)

But dec2bin returns the value represented in a character string rather than a number. If we want to implement your function and keep down this path of using the binary representation, we could do something like this:

b(1,:) = dec2bin(round(x(1)*q), bits);
for d = 2:bits
   b(d,:) = [b(d-1,2:end) '0'];
end

Each left-shift of the binary representation multiplies the value by 2. By ignoring the bit that's now to the left of the binary point, I'm implicitly performing the mod operation. Since we have no additional significant digits to add to the least-significant bit of the value, I just add a zero.

This will work; you get the proper values and can perform whatever operations on them you want. You can represent them as binary or decimal, you can turn them back into fractions, whatever.

But you can achieve the same thing without conversion to a binary representation.

x(1) = num / den;
for d = 2:bits
   x(d) = mod(x(d-1)*2, 1);
end

(Note that I left the value in x(1) as a fraction.) This does exactly the same operation on the exact same numbers. The one difference is that I didn't reduce the precision of the number at the beginning so it uses the full double precision. Now if I want to take these values and represent them as binary, I can still do that (remember to force the value to the integer range first, though).

c = dec2bin(round(x*q), bits);

Here's the result of a test run of both versions:

b =

11110000011101110111110010010001
11100000111011101111100100100010
11000001110111011111001001000100
10000011101110111110010010001000
00000111011101111100100100010000
00001110111011111001001000100000
00011101110111110010010001000000
00111011101111100100100010000000
01110111011111001001000100000000
11101110111110010010001000000000
11011101111100100100010000000000
10111011111001001000100000000000
01110111110010010001000000000000
11101111100100100010000000000000
11011111001001000100000000000000
10111110010010001000000000000000
01111100100100010000000000000000
11111001001000100000000000000000
11110010010001000000000000000000
11100100100010000000000000000000
11001001000100000000000000000000
10010010001000000000000000000000
00100100010000000000000000000000
01001000100000000000000000000000
10010001000000000000000000000000
00100010000000000000000000000000
01000100000000000000000000000000
10001000000000000000000000000000
00010000000000000000000000000000
00100000000000000000000000000000
01000000000000000000000000000000
10000000000000000000000000000000


c =

11110000011101110111110010010001
11100000111011101111100100100001
11000001110111011111001001000010
10000011101110111110010010000101
00000111011101111100100100001001
00001110111011111001001000010011
00011101110111110010010000100101
00111011101111100100100001001010
01110111011111001001000010010100
11101110111110010010000100101000
11011101111100100100001001010001
10111011111001001000010010100001
01110111110010010000100101000011
11101111100100100001001010000101
11011111001001000010010100001010
10111110010010000100101000010101
01111100100100001001010000101010
11111001001000010010100001010100
11110010010000100101000010100111
11100100100001001010000101001111
11001001000010010100001010011101
10010010000100101000010100111010
00100100001001010000101001110100
01001000010010100001010011101000
10010000100101000010100111010000
00100001001010000101001110100000
01000010010100001010011101000000
10000100101000010100111010000000
00001001010000101001110100000000
00010010100001010011101000000000
00100101000010100111010000000000
01001010000101001110100000000000

The two are identical except for the fact that b runs out of precision after 32 bits and c has 53 bits of precision. You can confirm this by running the code above but casting x(1) to a single :

x(1) = single(num / den);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM