I need my array to hit zero before starting next iteration
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
David Hill
on 29 Nov 2022
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
on 29 Nov 2022
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 Comments
David Hill
on 29 Nov 2022
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,:)
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!