for loop, in irregular numbers clusters?

4 次查看(过去 30 天)
i have to make a loop code, but the loopcounter (numbers) very irregular.
for an example: 1 2 3 4 6 7 8 10 12 13 14 15 16 18 19 20.
if i do it in bash script, the code would be like : [for idx in {1..8} 10 {12..16} {18..20}; do echo $idx; done;]
is there good way to code this case with matlab?
(above line is just for an example. in my practical job, the loop counter would go up to thousands.)
please share some wisdom. thank you.
p.s. : actually, in above example, when i loop from 1 to 20, there is a list of numbers "not to do" like [5, 9, 11, 17].
maybe i should make a array [1 ... 20] and delete out "not to do numbers" (5,9, 11,17) from loop...? i don't know how;;

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2022-10-20
编辑:Bjorn Gustavsson 2022-10-20
You will have these lists of "todos" and "not-todos" from somewhere outside, since, as you write, you can't type down all the numbers.
If you have the explicit array of numbers from outside it should be as simple as this:
function disp_orders(idx_to_do)
v = idx_to_do(:)';
for k = v
disp("Now serving order #" + k)
end
If you have the range of the numbers and an array of numbers to skip you can modify this easily:
function disp_orders(idx_range,idx_to_skip)
v = min(idx_range):max(idx_range); % Generate the full list
v = setdiff(v,idx_to_skip) % remove those not wanted
for k = v
disp("Now serving order #" + k)
end
Here you will obviously have to use the code-snippets inside the functions as suitable for your problem. This specifically addresses your question.
HTH
  2 个评论
Jong Yeon
Jong Yeon 2022-10-21
ah-ha. "setdiff( )" was the sweetie for me! thank you;;;
Bjorn Gustavsson
Bjorn Gustavsson 2022-10-21
You're welcome, happy that it helped.
There must be some kind of N%-M% rule for matlab-use, perhaps 80% of the time an existing matlab-function solves at least 80% of the problem - sometimes it is just a question about how to find them.

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2022-10-20
v = [1 2 3 4 6 7 8 10 12 13 14 15 16 18 19 20];
for k = v
disp("Now serving order #" + k)
end
Now serving order #1 Now serving order #2 Now serving order #3 Now serving order #4 Now serving order #6 Now serving order #7 Now serving order #8 Now serving order #10 Now serving order #12 Now serving order #13 Now serving order #14 Now serving order #15 Now serving order #16 Now serving order #18 Now serving order #19 Now serving order #20
  1 个评论
Jong Yeon
Jong Yeon 2022-10-20
thank you for first answer. but i can't type down all the numbers text, the numbers would be hundreds or thousands ...
numbers in list "not to serve" are not that many. so i can type down them.
i need to express it in other way. thank you any way ; )

请先登录,再进行评论。

类别

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

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by