I need my array to hit zero before starting next iteration
2 次查看(过去 30 天)
显示 更早的评论
I am working on this code, I am no expert at all in MATLAB. I would like for cases1 to reach zero before moving on to the next "diff_counts", but it will instead start over and never reach zero. How do I make this hapopen? any help is appreciated.
here is what the first values of cases1 look like incase I did not explain well.
diff_counts=[20 40 60 80 100];
cases1=zeros(1,10080);
starts1=sort(randi(10080,1000,1),'ascend');
for j=1:1000
start1(j)=starts1(j,1);
cases1(1,start1(j))=randsample(diff_counts,1);
x1=cases1(1,start1(j))/42.25;
for i=start1(j):10080
cases1(1,i+1)=cases1(1,i)-x1;
if cases1(1,i)<=0
cases1(1,i)=0;
end
end
end
0 个评论
回答(2 个)
David Hill
2022-11-29
Not sure if I completely understood your question, but break will break out of the for-loop.
diff_counts=[20 40 60 80 100];
cases1=zeros(1,10080);
starts1=sort(randi(10080,1000,1),'ascend');
for j=1:1000
cases1(starts1(j))=randsample(diff_counts,1);
x1=cases1(starts1(j))/42.25;
for i=starts1(j):10080
cases1(i+1)=cases1(i)-x1;
if cases1(i)<=0
cases1(i)=0;
break;
end
end
end
David Hill
2022-11-29
diff_counts=[20 40 60 80 100];
cases1=zeros(1,10080);
starts1=sort(randi(10080,1000,1),'ascend');
for j=1:1000
cases1(starts1(j))=randsample(diff_counts,1);
x1=cases1(starts1(j))/42.25;
i=starts1(j);
while cases1(i)>0
cases1(i+1)=cases1(i)-x1;
i=i+1;
end
cases1(i)=0;
end
cases1
8 个评论
David Hill
2022-11-29
d_counts=[20 40 60 80 100];
for k=1:numel(d_counts)
cases(k,:)=[d_counts(k):-d_counts(k)/42.25:0,0];%this generates the 5 cases
end
starts1=sort(randi(10080,1000,1));
s_cases=cases(mod(starts1,5)+1,:)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!