Hello everyone,
I have already calculated 25 values of X.
Now I am calculating next 10 values of X and I want to run a loop for that. Now when I am computing X(26) than I want to break the loop at the moment when the computed value of X(26) is having a difference of 1 with any of the already calculated 25 values of X. Now when I am calculating the next X(27) I want the same condition to follow as for X(26) but the code should not include that value of X from the already calculated 25 values of X which was used in calculating X(26).

댓글 수: 4

Try
for k = 26: 35
differences = X(k) - X(1:25);
if any(differences == 1)
break;
end
end
And if you're up to 27 or 30 or 35, which of the prior values will you consider when you're NOT taking any of the 25 values already used?
Prakhar Modi
Prakhar Modi 2019년 7월 13일
all the prior value except the one which is used to calculate the difference in X(26). It means for X(27) I should be calculating the difference with 24 values only and similarly for next X(28) their will 23 values only.
I don't follow that last...think we need an example of just what your intent is with numbers to illustrate.
But, the general logic as IA outline will be the right thing; you just need to be able to define a variable i1,i2 for each iteration that defines the subset of X to be compared to X(k) as
differences=X(k)-X(i1:i2);
...
i1=NEW_i1; % whatever logic it is that decides
i2=NEW_i2; % how these two change between iterations
...
Steven Lord
Steven Lord 2019년 7월 14일
Can you take a step back and explain at a higher level your goal in generating this data? There may be a more efficient algorithm we can suggest if we get the full picture of what you want to achieve.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2018a

태그

질문:

2019년 7월 13일

댓글:

2019년 7월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by