Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Is there a way to iterate this?

1 回表示 (過去 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