Executing loop on certain numbers
1 回表示 (過去 30 日間)
古いコメントを表示
Raghavendra Reddy P
2015 年 5 月 11 日
コメント済み: Raghavendra Reddy P
2015 年 5 月 11 日
i have two vectors L=1:12; idx=[3 5 9]; I wanted to execute for loop for i=12:-1:1 now i want execute vector idx, starting from last elemet to first i.e 9,5,3 something like
if i==9
do stuff
end
if i==5
do stuff
end
.
.
if i==3
do stuff
end
end
i don't know how many elements in vector idx before hand.
0 件のコメント
採用された回答
Stephen23
2015 年 5 月 11 日
編集済み: Stephen23
2015 年 5 月 11 日
idx = [3,5,9];
for k = idx(end:-1:1)
... code here
end
3 件のコメント
Stephen23
2015 年 5 月 11 日
編集済み: Stephen23
2015 年 5 月 11 日
for k = idx(end:-1:1)
switch k
case 9
... code
case 5
... code
case 3
... code
otherwise
error('This values is not known: %d',k)
end
end
But, depending on what those calculations are, there might be much easier and faster ways of achieving this. If you tell us exactly what the do stuff operations actually are, then we might be able to tidy this up too. Can the the vector idx change, or is it always the same?
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!