필터 지우기
필터 지우기

for loop, in irregular numbers clusters?

조회 수: 2 (최근 30일)
Jong Yeon
Jong Yeon 2022년 10월 20일
댓글: Bjorn Gustavsson 2022년 10월 21일
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