for loop with exceptions

조회 수: 6 (최근 30일)
cgo
cgo 2018년 1월 8일
답변: Jan 2018년 1월 8일
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일
loopCounter = [2,6:2:26]
for i = loopCounter
i
% your code
end

Jan
Jan 2018년 1월 8일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by