For loop: access values in between steps

11 ビュー (過去 30 日間)
Simon Allosserie
Simon Allosserie 2021 年 1 月 21 日
コメント済み: Yvan Lengwiler 2021 年 1 月 21 日
Hi
I have a for-loop where first only certain rows of a matrix are dealt with:
for i = 1:3:100 %3 is an example step size, the step size is a variable in the program
%do something with rows 1, 4, 7, 10, 13, 16 ...
end
After this, the remaining rows, i.e. rows 2,3, 5,6, 8,9, ... need to be filled up using the data in the rows calculated above. I don't see which would then be the most optimal method to define this new for loop. Possibly, for loops aren't even the best way to approach this? Thanks for your input!
  2 件のコメント
Mathieu NOE
Mathieu NOE 2021 年 1 月 21 日
hello Simon
if your computations can be vectorize there is no need to do for loops here
now, if you have to do more complicated stuff like doing ode solving for example , there is no other way than doing it with a for loop
it all depends on what the computation is about
Simon Allosserie
Simon Allosserie 2021 年 1 月 21 日
Hey Mathieu
The computations cannot be vectorized, I fear.
There is a small "scanning matrix" that hovers over the big matrix in which I adapt the rows depending on the interactivity between the data in the small and the large matrix, and this for each cell in the selected rows.

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

採用された回答

Yvan Lengwiler
Yvan Lengwiler 2021 年 1 月 21 日
for i = 1:3:100
%do something with rows 1, 4, 7, 10, ...
end
for i = 2:3:100
%do something with rows 2, 5, 9, 11, ...
end
for i = 3:3:100
%do something with rows 3, 6, 10, 12, ...
end
  4 件のコメント
Simon Allosserie
Simon Allosserie 2021 年 1 月 21 日
Yes that's it! I was looking for a way to note it all in one for-loop, but this nested loop does te trick. Thanks, Yvan!
Yvan Lengwiler
Yvan Lengwiler 2021 年 1 月 21 日
You're welcome :-)

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

その他の回答 (0 件)

カテゴリ

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