How to eliminate the for in the following function and make it vectorized, should be simple?
Afficher commentaires plus anciens
Hello Experts,
I need to generate a sequence of integers X_i; i = 1:100000 within the interval 0 to 134455 using the recursion:
X_i+1 = 8121*X_i + 28411 mod 134456.
What I did is this:
X = zeros(100000,1);
i = 1;
X(1,1) = randi(134455);
for i=1:100000
X(i+1,1) = mod(8121*X(i,1) + 28411,28411);
end
I wanted to make it a bit more tricky and maybe eliminate the for loop. Please guide me how to do this quick and easy.
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 3 Nov 2013
X is a floating point number. To be sure you don't get bitten by this FAQ entry, create X as integer:
X = zeros(100000, 1, 'int32');
"A bit more tricky" and "quick and easy" seem like opposites. I think you've already made it as complicated as necessary since you could have just called randi() and get the whole sequence of random numbers in one single line of code.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!