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

回答(2 个)

David Hill
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
  1 个评论
Noah Varner
Noah Varner 2022-11-29
Im aware of what break does, dont think that solves my problem though. I need the value of cases1 to be zero before the script goes to the next "starts1"

请先登录,再进行评论。


David Hill
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
cases1 = 1×10121
0 100.0000 20.0000 19.5266 19.0533 18.5799 18.1065 17.6331 80.0000 78.1065 76.2130 74.3195 72.4260 70.5325 60.0000 58.5799 57.1598 55.7396 54.3195 52.8994 51.4793 50.0592 48.6391 47.2189 45.7988 44.3787 42.9586 41.5385 40.1183 20.0000
  8 个评论
David Hill
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,:)
s_cases = 1000×44
100.0000 97.6331 95.2663 92.8994 90.5325 88.1657 85.7988 83.4320 81.0651 78.6982 76.3314 73.9645 71.5976 69.2308 66.8639 64.4970 62.1302 59.7633 57.3964 55.0296 52.6627 50.2959 47.9290 45.5621 43.1953 40.8284 38.4615 36.0947 33.7278 31.3609 80.0000 78.1065 76.2130 74.3195 72.4260 70.5325 68.6391 66.7456 64.8521 62.9586 61.0651 59.1716 57.2781 55.3846 53.4911 51.5976 49.7041 47.8107 45.9172 44.0237 42.1302 40.2367 38.3432 36.4497 34.5562 32.6627 30.7692 28.8757 26.9822 25.0888 60.0000 58.5799 57.1598 55.7396 54.3195 52.8994 51.4793 50.0592 48.6391 47.2189 45.7988 44.3787 42.9586 41.5385 40.1183 38.6982 37.2781 35.8580 34.4379 33.0178 31.5976 30.1775 28.7574 27.3373 25.9172 24.4970 23.0769 21.6568 20.2367 18.8166 20.0000 19.5266 19.0533 18.5799 18.1065 17.6331 17.1598 16.6864 16.2130 15.7396 15.2663 14.7929 14.3195 13.8462 13.3728 12.8994 12.4260 11.9527 11.4793 11.0059 10.5325 10.0592 9.5858 9.1124 8.6391 8.1657 7.6923 7.2189 6.7456 6.2722 100.0000 97.6331 95.2663 92.8994 90.5325 88.1657 85.7988 83.4320 81.0651 78.6982 76.3314 73.9645 71.5976 69.2308 66.8639 64.4970 62.1302 59.7633 57.3964 55.0296 52.6627 50.2959 47.9290 45.5621 43.1953 40.8284 38.4615 36.0947 33.7278 31.3609 80.0000 78.1065 76.2130 74.3195 72.4260 70.5325 68.6391 66.7456 64.8521 62.9586 61.0651 59.1716 57.2781 55.3846 53.4911 51.5976 49.7041 47.8107 45.9172 44.0237 42.1302 40.2367 38.3432 36.4497 34.5562 32.6627 30.7692 28.8757 26.9822 25.0888 60.0000 58.5799 57.1598 55.7396 54.3195 52.8994 51.4793 50.0592 48.6391 47.2189 45.7988 44.3787 42.9586 41.5385 40.1183 38.6982 37.2781 35.8580 34.4379 33.0178 31.5976 30.1775 28.7574 27.3373 25.9172 24.4970 23.0769 21.6568 20.2367 18.8166 40.0000 39.0533 38.1065 37.1598 36.2130 35.2663 34.3195 33.3728 32.4260 31.4793 30.5325 29.5858 28.6391 27.6923 26.7456 25.7988 24.8521 23.9053 22.9586 22.0118 21.0651 20.1183 19.1716 18.2249 17.2781 16.3314 15.3846 14.4379 13.4911 12.5444 80.0000 78.1065 76.2130 74.3195 72.4260 70.5325 68.6391 66.7456 64.8521 62.9586 61.0651 59.1716 57.2781 55.3846 53.4911 51.5976 49.7041 47.8107 45.9172 44.0237 42.1302 40.2367 38.3432 36.4497 34.5562 32.6627 30.7692 28.8757 26.9822 25.0888 20.0000 19.5266 19.0533 18.5799 18.1065 17.6331 17.1598 16.6864 16.2130 15.7396 15.2663 14.7929 14.3195 13.8462 13.3728 12.8994 12.4260 11.9527 11.4793 11.0059 10.5325 10.0592 9.5858 9.1124 8.6391 8.1657 7.6923 7.2189 6.7456 6.2722

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Descriptive Statistics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by