for loop, in irregular numbers clusters?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Jong Yeon
il 20 Ott 2022
Commentato: Bjorn Gustavsson
il 21 Ott 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;;
0 Commenti
Risposta accettata
Bjorn Gustavsson
il 20 Ott 2022
Modificato: Bjorn Gustavsson
il 20 Ott 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 Commenti
Bjorn Gustavsson
il 21 Ott 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.
Più risposte (1)
Steven Lord
il 20 Ott 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
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!