Effacer les filtres
Effacer les filtres

for loop, in irregular numbers clusters?

2 vues (au cours des 30 derniers jours)
Jong Yeon
Jong Yeon le 20 Oct 2022
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;;

Réponse acceptée

Bjorn Gustavsson
Bjorn Gustavsson le 20 Oct 2022
Modifié(e) : Bjorn Gustavsson le 20 Oct 2022
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 commentaires
Jong Yeon
Jong Yeon le 21 Oct 2022
ah-ha. "setdiff( )" was the sweetie for me! thank you;;;
Bjorn Gustavsson
Bjorn Gustavsson le 21 Oct 2022
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 20 Oct 2022
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 commentaire
Jong Yeon
Jong Yeon le 20 Oct 2022
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 ; )

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by