Generating Number from a given set with given probability in Matlab [duplicate] - matlab

This question already has answers here:
Draw random numbers from pre-specified probability mass function in Matlab
(1 answer)
Generate random number with given probability matlab
(7 answers)
Closed 6 months ago.
The community reviewed whether to reopen this question 6 months ago and left it closed:
Original close reason(s) were not resolved
I need to generate 5 integer number which must be from a given set of numbers {1,2,4,8,16}.
I need different values to have different probabilities of generating eg.
0.10 chance of 1
0.15 chance of 2
0.30 chance of 4
0.25 chance of 8
0.20 chance of 16
How can I do this in Matlab?

You can simply draw a random number between 0 and 1. Then assign each interval one of your numberset. E.g.: random number is 0.12 -> your number will be 2.
The first 0.0 to 0.1 will be therefor 1, 0.1 to 0.15 will be 2 and so on...

Related

Matlab Rounding Computation Error [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 5 years ago.
Under Matlab 2014b, when making:
round((0.1:0.2:1)/0.2)
I obtain:
1 2 3 3 5
Instead of
1 2 3 4 5
Is there a way to fix such weird computation? Why this happens? Why so weird? This repeats for other values, not only n=0.2.
EDIT: I checked the duplicate but, do the scenario changes when using ceil so the floating point threshold is not in the half integer (0.5, 1.5, 2.5) but in the integer (1.0, 2.0, 3.0)? What if i use some other representation, like single? Or any other more predictable?
since the question got reopened, I'll briefly mention that adding 1.3+0.2 with floats can result in 2.49999999999 or in 2.5000000001 etc. the round will act according to the floating point error.

Generating random numbers rounded to hundredths place in MATLAB [duplicate]

This question already has answers here:
MATLAB generate random numbers
(5 answers)
Closed 6 years ago.
I am trying to generate random numbers from 0.1 - 0.7.
My current code is close to what I need, but is giving me numbers lower than 0.1:
frameDur1 = roundn((((7)*rand(1))/10), -2);
If you have suggestions on another/easier function I could use, that would be appreciated as well.
Thanks in advance!
*Don't believe this question is a duplicate, but thanks to Ali Rokni for the answer.
rand generate uniformly distributed random numbers between (0,1).
Your range size is
0.7 - 0.1 = 0.6 % max - min
First you should change the range to (0, 0.6). And then you add the minimum for changing the offset.
Therefore ultimately you have
0.1 + (0.7 - 0.1) * rand
Generally speaking assuming your desired range is (min, max)
min + (max - min) * rand

MATLAB showing 0.82-0.80 not equal to 0.02 and similar error in inverting a matrix [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 7 years ago.
I've written the following code*:
function val = newton_backward_difference_interpolation(x,y,z)
% sample input : newton_backward_difference_interpolation([0.70 0.72 0.74 0.76 0.78 0.80 0.82 0.84 086 0.88],[0.8423 0.8771 0.9131 0.9505 0.9893 1.0296 1.0717 1.1156 1.1616 1.2097],0.71)
% sample output:
% X-Input must be equidistant
% -1.1102e-16
n=numel(x);% number of elements in x (which is also equal to number of elements in y)
h=x(2)-x(1);% the constant difference
%error=1e-5;% the computer error in calculating the differences [It was workaround where I took abs(h-h1)>=error]
if n>=3% if total elements are more than 3, then we check if they are equidistant
for i=3:n
h1=x(i)-x(i-1);
if (h1~=h)
disp('X-Input must be equidistant');
disp(h1-h);
return;
end
end
end
...
I also wrote a function for calculating inverse of a matrix** where this:
a=[3 2 -5;1 -3 2;5 -1 4];disp(a^(-1)-inverse(a));
displays this:
1.0e-16 *
0 -0.2082 0.2776
0 0.5551 0
0 0.2776 0
Why is there a small difference of 1e-16 occuring? Am I right in saying that this is because computer calculations (especially divisions are not exact)? Even if they are inexact, why does a^(-1) provide more accurate/exact solution (Sorry If this question is not programming related in your view.)
Can this be avoided/removed? How?
*for newton backward difference interpolation: (whole code at this place), the expected output is 0.85953 or something near that.
**using Gauss-Jordan Elimination: (whole code at this place)
That's in the nature of floating point calculations. There habe been many questions about this topic here already.
E.g.
Floating point inaccuracy examples
What range of numbers can be represented in a 16-, 32- and 64-bit IEEE-754 systems?
https://www.quora.com/Why-is-0-1+0-2-not-equal-to-0-3-in-most-programming-languages
The problem is that non-integer numbers in most cases have no exact representation by a 64bit double or 32bit float, not even in a 10000bit floating point value if that existed.
0.5, 0.25 and all powers of 2 (also those with negative exponents, 0.5 is 2^-1) can be stored exactly, and many others as well. Floating point numbers are saved as 3 separate parts: sign (1bit), mantissa (the 'number') and exponent for the base 2. Every possible combination of mantissa and exponent will result in a precise value, e.g. 0.5 has mantissa 'value' of 1 and exponent -1. the exact formula is
number = (sign ? -1:1) * 2^(exponent) * 1.(mantissa bits)
from
http://www.cprogramming.com/tutorial/floating_point/understanding_floating_point_representation.html
Try this formula and you'll see for example that you can't create a precise 0.1

4 random numbers totaling 100 [duplicate]

This question already has answers here:
Split a random value into four that sum up to it
(7 answers)
Closed 7 years ago.
I want to generate 4 random numbers between 1 and 100 so that the total adds up to 100. How do I do that?
say result1) 20,10,40,30
result2 ) 33,33,33,1
etc. Thanks
The way to go is:
Step 1: Generate four random numbers between 0 and 1
Step 2: Add these four numbers
Step 3: Divide each of the four numbers by the sum,
Step 4: Multiply by 100, and round to the nearest integer.

Generate 1 random number between -1 and 1 in Matlab [duplicate]

This question already has answers here:
Is there a way in Matlab using the pseudo number generator to generate numbers within a specific range?
(2 answers)
Closed 7 years ago.
I am new to matlab and I need to add one random number between -1 and 1 to the equation. I also need to generate a random number between -5 and 5.
I need float number not int
For a random double between -1 and 1:
a = 2*rand(1)-1;
And between -5 and 5:
b = 5*(2*rand(1)-1);
Best,