I need my array to hit zero before starting next iteration
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
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
1 Comment
Noah Varner
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
Noah Varner
on 29 Nov 2022
David Hill
on 29 Nov 2022
Please show me an example of your desired output. The way you have it set up, they are going to over lap. Big picture what are you trying to do?
Noah Varner
on 29 Nov 2022
diff_counts=[20 40 60 80 100];
cases1=zeros(1,10080);
starts1=1:45:10080;%starts1 need to be separated by about 44 so as to not overlap
for j=1:numel(starts1)
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
Noah Varner
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
%it does not matter what the start time is, the case is just going to be
%one of the five cases randomly selected.
s_cases=cases(randi(5,1,1000),:)
Noah Varner
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,:)
Categories
Find more on Clocks and Timers 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!