execution of for loops on specific numbers

k=[3 8 10 12]
i wanted to execute above mentioned indexes in for loop something like this
for i=[3 8 10 12]
if i==3
do stuff
end
if i==10
do stuff
.
.
.
if i==12
do stuff
end
end
i don't know vector k elements before hand.

댓글 수: 3

I don't fully understand the question. Something like
k=[3 8 10 12];
for cnt=k
if k==k(1)
%do something 1
elseif k==k(2)
%do something 2
elseif k==k(3)
%do something 3
elseif k==k(4)
%do something 4
end
end
is the same as
do something 1
do something 2
do something 3
do something 4
which is obviously a bit shorter. Could you please show what exactly you want?
Guillaume
Guillaume 2015년 5월 11일
편집: Guillaume 2015년 5월 11일
@Michael, yes it's the same for a hardcoded k, but if k is an input to the function, then it's not:
k = randi(3, 1, 5);
for action = k
switch action
case 1
disp('do action 1');
case 2
disp('do action 2');
case 3
disp('do action 3');
end
end
Like you though, I've no idea what Raghavendra is asking.
i think he has already got the answer through other post.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 5월 11일

댓글:

2015년 5월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by