Info

此问题已关闭。 请重新打开它进行编辑或回答。

Is there a way to iterate this?

2 次查看(过去 30 天)
Claire
Claire 2019-11-9
关闭: MATLAB Answer Bot 2021-8-20
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 个评论
Walter Roberson
Walter Roberson 2019-11-9
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 2019-11-12
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.

回答(0 个)

此问题已关闭。

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by