About Random Number Generator in Matlab - matlab

The rand function in matlab generates a random number between 0-1 with space 0.0001. Is there a way to widen this space so that the number generated is only to the first or second decimal place?? Thanks for any suggestion.

You can generate pseudo-random integers between [0,10^n] using randi and divide by 10^n:
randTens = randi([0,10 ])/10;
randHundreds = randi([0,100])/100;
...

You can use round function:http://www.mathworks.com/help/matlab/ref/round.html
Y = round(X,2)
Y = round(X,N) rounds to N digits:
N > 0: round to N digits to the right of the decimal point.
N = 0: round to the nearest integer.
N < 0: round to N digits to the left of the decimal point.

I understand Matlab generates uniformly spaced random numbers. If you want to widen the precision step, you could try multiplying rand by some value 'a' giving you 0.0001*a spacing and then selecting the numbers that are inferior to 1.

Related

MATLAB - Generating a rand value vector (each value [0; 1] with at least X% zeros)?

Does anybody know an efficent way to retrieve such a result?
I could of course use rand(n,1) and then replace by iterating over the array values with zeros until the number of zeros is sufficient. (as written above atleast X% zeros it can also be more but not less)
I would prefer a completly random distribution but also a uniform distribution would be fine. (So I have no real clue how the distribution effects the result)
(Currently using MATLAB 2017a)
Use A=rand(n); to generate random vector, then use num = randi([n*proc,n]); to figure out how many of the numbers should be changed for 0 (proc is the minimum fraction of numbers which should be 0). Substitute for 0 with A(1:num)=0; and then shuffle the array with B = A(randperm(n));.
In total:
A=rand(n);
proc = 0.3;
num = randi([n*proc,n]);
A(1:num)=0;
B = A(randperm(n));

MATLAB is rounding my values to 1

n = 215;
N = 215.01:0.1:250;
p = 0.52;
q = 0.48;
Gamblers = (1 - (q/p)^n)./(1 - (q/p).^N);
plot(Gamblers)
Matlab takes the numerators and denominators as simply 1, filling the array with nothing but that value. How can I correct this?
Your denominator and numerator are very close to 1 but not exactly 1. The plot(Gamblers) confirms this.
By default MATLAB will display numbers with four digits after the decimal place. Your numerator is 0.999999966414861, which with four digits rounds to 1. MATLAB uses double precision numbers so your calculation here is still accurate.
Try double clicking on the Gamblers variable to open the variables window and then double clicking on one of the results. You'll see it change from the default display precision to a much more accurate depiction of your variable.

MATLAB - How do I find the first integer of an infinite set that satisfies this condition?

I want to find the smallest integer P, such that the number of primes in the set {1,2,..., P} is less than P/6.
I think have the answer via (long) trial and error but would like to know how to verify this through MATLAB.
You can use isprime to check if any value in an array is a prime number. If we want to check all integers up until the integer N we can do
% You can change this to the maximum number that you'd like to consider for P
N = 2000;
possible_P_values = 2:N; % We omit 1 here since it's not a prime number
primes = isprime(possible_P_values);
To determine how many primes have occured up to a given integer N we can use cumsum of this logical matrix (the cumulative sum)
nPrimes_less_than_or_equal_to_P = cumsum(primes);
Then we can divide possible_P_values by 6 and check where the number of primes up to a certain point is less than that number.
is_less_than_P_over_6 = nPrimes_less_than_or_equal_to_P < (possible_P_values ./ 6);
Then we can identify the first occurance with find
possible_P_values(find(is_less_than_P_over_6, 1, 'first'))
% 1081

Given a positive number , how to determine its surrounding integers in matlab

Can someone please explain how to get the surrounding integers for a given positive number ( for ex. if number is 18.2378 then it should return 18 and 19 )
(I actually need this to determine that the given number is between 0-1 or 2-3 or 4-5 and so on....and if it is in between 0-1 or 2-3 or 4-5 etc then some expression evaluates , else some other expression must evaluate.)
The floor and ceil functions do this:
x = 18.2378;
floor(x); %Returns 18
ceil(x); %Returns 19
floor(18.2378) will return 18 i.e. the previous nearest integer.
ceil(18.2378) will return 19 i.e. the next nearest integer
You can use round or floor or ceil in Matlab to turn decimal numbers into integers. Round will round up or down depending on the decimal value, floor rounds toward minus infinity, and ceil rounds toward positive infinity.
Here is an example of how this could work:
n=18.2378;
F=floor(n);
C=ceil(n);
TF=F<n<C;
F returns 18. C returns 19. TF will return 1 if the number is between the floor and the ceiling--But, if you do it this way the number will always be between its floor and ceiling--and 0 if it is not. You can do this iteratively in a loop as many times as you need.

Calculating the product of all the odd numbers

So I am trying to create a script that calculates the product of all the odd numbers from 1 to 1000 (using MATLAB). The program runs but the product is not correct:
%Program is meant to calculate the product of all the odd numbers from 1 to 1000
% declare variable ‘product’ as zero
product = 0.;
% initialize counter, ‘n’, to 1000
n = 1000;
for i = 1:2:n
product = product + i;
end
fprintf('The product of all the odd numbers from 1 to %d is %d\n', n, product)
So I'm not really sure how to go about this and am looking for some guidance. Thanks!
Solution
Currently, your script is set to add all of the odd numbers from 1 to 1000.
To perform the product, you just need to change the starting value of product to 1 and multiply within the loop:
product = 1;
for i = 1:2:1000
product = product * i;
end
However, it is faster to create a vector and have the built-in prod function perform the multiplication:
product = prod(1:2:1000);
Problem
MATLAB does not by default have enough memory in the default 64-bit numbers to compute the exact value of this product.
The number is too large since this is essentially a factorial.
You'll find that MATLAB returns Inf for the 500 numbers you're multiplying, and it is only finite for up to 150 elements.
In fact, using floating point arithmetic, the number is only accurate up to 15 digits for the first 17 digits using floats (integers saturate at that level as well).
Using Mathematica (which can perform arbitrary digit arithmetic out-of-the-box since I'm feeling lazy), I can see that the answer needs at least 1300 digits of precision, which we can have MATLAB do through the Symbolic Toolbox's vpa function:
digits(1300);
p = vpa(1);
pint = vpa(1);
for k = 2:N
pint = pint*p(k);
end
disp(pint);
>> StackOverflow
100748329763750854004038917392303538250323418583550415705013777513334847930864905026212149922688916514224446856302103818809813965739969905602683824057028542369814437703275217182106137628427025253936696857063927677887236450311036887007989218384076420973974651860279864376153012567675767840733574225799002463604490891982796305162134708837541147007332276627034016790073315219533088052639255340728943149219519187498959529434982654113006616219355830114439411562650611374970334868978510289340267833632215930432706056111069583472778227977585526504938921664232801595705593340414168289146933191250605578218896799783237156997993612173843567447982392426109444012350386990916069363415575527636429080027392875413821124412782341957015410685185402984322002697631153866494712956244870206835064084512590679022924697003630949759950902438767963278695296882620493296103779237046934780464541286585179975172680371269700518965123152181467825566303777704391998857792627009043170482928030252033752456172692668989206857862233381387134495504231267039972111966329704875185659372569246229419619030694680808504265784672316785572965414328005856656944666840982779185954031239345256896720409853053597049715408663604581472840976596002762935980048845023622727663267632821809277089697420848324327380396425724029541015625.0