for loop with exceptions

I wanted to run the loop
for even = 2:2:26
but I don't want to include 4. how do i say this?

回答 (2 件)

Pawel Jastrzebski
Pawel Jastrzebski 2018 年 1 月 8 日
編集済み: Pawel Jastrzebski 2018 年 1 月 8 日

0 投票

loopCounter = [2,6:2:26]
for i = loopCounter
i
% your code
end
Jan
Jan 2018 年 1 月 8 日

0 投票

for even = 2:2:26
if even ~= 4
disp(even)
end
end
Or
index = 2:2:26;
index(index == 4) = [];
for k = index
disp(k);
end
Or for a larger list of excluded variables:
index = 2:2:26;
index = setdiff(index, [4,8,18]);
for k = index
disp(k);
end

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

cgo
2018 年 1 月 8 日

回答済み:

Jan
2018 年 1 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by