How to tell loop how many values to skip?

6 ビュー (過去 30 日間)
MC3105
MC3105 2014 年 9 月 28 日
編集済み: MC3105 2014 年 9 月 28 日
Hello everyone!
I want to create a loop similar to:
t=1:24:300
BUT I don't want the loop to skip 24 values after every run-through. After each run-through I want to tell the loop exactly how many values it should skip.
For example: The first run-through should be from 1:24, the second should be from 25:30, the third should be from 31:50.... and the last one from 277:300.
Is this possible in matlab?
  2 件のコメント
José-Luis
José-Luis 2014 年 9 月 28 日
編集済み: José-Luis 2014 年 9 月 28 日
Do you know the values beforehand or will they be calculated inside the loop? Also, I don't really understand what you mean by 25:30, 31:50, etc...
MC3105
MC3105 2014 年 9 月 28 日
I just found a solution :)
I can do
t=1:s:300
then I can calculate the number of steps to skip within the loop :)

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2014 年 9 月 28 日
This seems to do what you want:
X = linspace(0, 10, 300); % Create Data Vector
t = [0 24 30 50 276 300]; % Partition Index Vector
for k1 = 1:length(t)-1
Y{k1} = X(t(k1)+1:t(k1+1));
end
The cell array ‘Y’ has your partitioned sections from vector ‘X’. Create ‘t’ to be whatever values you want. I used the numbers you defined in your Question in this example.
  1 件のコメント
MC3105
MC3105 2014 年 9 月 28 日
編集済み: MC3105 2014 年 9 月 28 日
unfortunately i don't know beforehand how my intervals will look like..
but I just found my solution: it's possible to do t=1:s:24 and then calculate the number of steps that the loop should skip before the next run-through as s within my loop :)

サインインしてコメントする。

その他の回答 (1 件)

Marc
Marc 2014 年 9 月 28 日
It is definitely possible but your logic is not very clear. Since you are choosing odd intervals, is there some underlying reason for 1 thru 24 followed by 25 thru 30, etc. etc.
You could use if/else-if/else or switch/case or while but we would need to have some condition met to move to the next run, and so on.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by