How can I remove for loops?

3 vues (au cours des 30 derniers jours)
Griselda
Griselda le 25 Oct 2013
Commenté : Griselda le 25 Oct 2013
Hello. I would like to remove the for loops from this script. Could anyone provide me information on how to do it? The loop takes a long time to run. Thank you!
n=12000;
resamples=5000;
Z=zeros(1,5);
Y=zeros(resamples,5);
for j=1:resamples
for k=1:n
r=randi(n);
Z=Z+B(r,:);
end
Y(j,:)=Z;
end
  1 commentaire
Matt J
Matt J le 25 Oct 2013
Modifié(e) : Matt J le 25 Oct 2013
Please format your code readably, using the
toolbar button.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 25 Oct 2013
idx=randi(n,n*resamples,1);
Y=sum(reshape(B(idx,:),n,[]),1);
Y=reshape(Y,resamples,[]);
  1 commentaire
Griselda
Griselda le 25 Oct 2013
Thak you very much. Griselda

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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!

Translated by