Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Is there a way to iterate this?

2 vues (au cours des 30 derniers jours)
Claire
Claire le 9 Nov 2019
Clôturé : MATLAB Answer Bot le 20 Août 2021
I'd like to run this code 1000 times -- i.e., generate 1000 xy_mc matrices, each containing "N_mc" X, Y coordinates. I have tried to add a "for k=1:L" loop (where L =1000) before and after the for i = 1:N_mc loop. But, when I iterate xy_mc ( "xy_mc(k) =newpt_mc()" ) I get an error message. I also tried iterating the preallocation of storage within the loop as an attempt at creating mulitple xy_mc matrices. I also tried doing r_mc(i,k) throughout this code.
Any suggestions are appreciated. Thank you.
N_mc = 220
m_mc = size(N_mc,1);
n_mc = size (N_mc,2);
r_mc = zeros(m_mc,n_mc);
xy_mc = zeros(m_mc,n_mc);
for i=1:N_mc
r_mc(i) = normrnd(9,0.5); % choose random radius
newpt_mc = @()r_mc(i)+ (1063-2*r_mc(i))* rand([1,2]);
xy_mc() = newpt_mc(); % matrix for store XY coordinates
fails = 0; % avoid looping forever
while size(xy_mc,1) < N_mc
pt = newpt_mc();
if all(pdist2(xy_mc, pt) > 2*r_mc(i))
xy_mc = [xy_mc(); pt]; % add it
fails = 0; % reset failure counter
else
fails = fails + 1;
if fails > 5000
error('taking too long');
end
end
end
end
  2 commentaires
Walter Roberson
Walter Roberson le 9 Nov 2019
xy_mc() = newpt_mc();
is not valid syntax no matter whether it is being looped or not. The left hand side of an assignment end in () .
Claire
Claire le 12 Nov 2019
Okay. I will change that.
Is there a way to run this loop 1000 times?
Would it be possible to save this code as a function, and run the funciton 1000 times?
Or, is a parfor loop possible?
These are both things I have not used before. Any advice is appreiciated.

Réponses (0)

Cette question est clôturée.

Produits


Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by