Elegant way to know in which iteration of for I am

8 ビュー (過去 30 日間)
Inês
Inês 2014 年 7 月 23 日
編集済み: per isakson 2014 年 7 月 24 日
At the moment I have something of the sort:
n_runs=0;
N_categories=[17, 8];
for protocol_ID=[4 1 7 6]
for t=1:nTrialsPerBlock
n_runs=n_runs+1;
if n_runs > N_categories
(--code--)
end
(---code---)
end
end
What I am looking for is a way to evaluate in which protocol the first for is (meaning if it is in 4, 1, 7 or 6) and then if it is in the first two, should be n_runs > N_categories( 1 ) and if it's the second pair N_categories( 2 ).
I am trying to code it so that it determines in which protocol I am, and according to that evaluate if the number of runs is bigger than the corresponding N_categories. Note that protocol_ID could have any of the numbers (4,1,7,6) in no particular order.
I am not seeing a better way than a bunch of switches/if's. Anyone could give a hand? :)
  3 件のコメント
Inês
Inês 2014 年 7 月 23 日
I was looking for something other than switch because the instructions on each pair would be equal here. Can a switch work if I do:
switch protocol_ID
case 1 || 4
case 6 || 7
?
per isakson
per isakson 2014 年 7 月 23 日
See the answer of dpb below.

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

回答 (3 件)

dpb
dpb 2014 年 7 月 23 日
...Can a switch work if I do:
switch protocol_ID
case 1 || 4
case 6 || 7
Not in that exact syntax, no, but
switch protocol_ID
case {1, 4}
...
case {6, 7}
...
otherwise
...
doc switch % etc., for details...

dpb
dpb 2014 年 7 月 23 日
I don't follow what the end objective is from the description, but to know which iteration numerically is being evaluated you've got the indicator variable being incremented in the wrong place--
iter=0;
N_categories=[17, 8];
for protocol_ID=[4 1 7 6]
iter=iter+1;
if round(iter/2)==1
% stuff for first two
else
% second two
end
...
  1 件のコメント
Inês
Inês 2014 年 7 月 23 日
編集済み: Inês 2014 年 7 月 23 日
Yeah I didn't explain it right, sorry. The thing is protocol_ID is meant to be changed into any order, so it could also be [6, 4, 7, 1] or [4, 1] for example, depending on what the user wants. What I want is to know in which protocol_ID the loop is at the moment, to know which stuff to do. It happens that 4/1 and 6/7 should have the same instructions.
So you see, just counting iterations wouldn't work because I don't want to know how many, just what the current protocol is.

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


lvn
lvn 2014 年 7 月 23 日
編集済み: lvn 2014 年 7 月 23 日
This should do it:
if (protocol_ID==1 || protocol_ID==4) && n_runs > N_categories( 1 )
..
elseif (protocol_ID==6 || protocol_ID==7) && n_runs > N_categories( 2 )
..
end

カテゴリ

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