Populate a vector to a specified threshold limit
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi; I'm trying to generate an array by calculating 'y from a large list of random numbers 'x', but I want the vector to stop populating once it reaches 5000 entries.
Here's the code to generate the vector (and reject all values less than -5 and greater than 25)
a=10; T=3;
x = rand(10000,1);
y=a+((T/2).*(tan(pi.*(x-(0.5)))));
y(y<-5)=[];
y(y>25)=[];
So this generates entries in the vector 'y', but I want the vector to stop populating after it reaches 5000 entries. I've tried using a while loop and playing around with counting the entries in the array then trying to break out of the loop, but I can't seem to get it to work. Any help is appreciated! Thanks!
0 commentaires
Réponse acceptée
Fangjun Jiang
le 9 Nov 2011
Can you generate a reasonably large random number, e.g. x=rand(50000,1), then after y(y<-5)=[] and y(y>25)=[] operation, it is almost certain that y is more than 5000 in length? Then you can do y(5001:end)=[].
It is certainly possible to do it using a while-loop, but probably won't be fast.
a=10; T=3;
y=zeros(5000,1);
k=1;
while k<=5000
x= rand;
temp=a+((T/2).*(tan(pi.*(x-(0.5)))));
if -5<=temp && temp<=25
y(k)=temp;
k=k+1;
end
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Types dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!