フィルターのクリア

How do I create a for loop with mutiple conditions in MATLAB?

4 ビュー (過去 30 日間)
Karen
Karen 2023 年 6 月 29 日
コメント済み: Karen 2023 年 6 月 29 日
Hi, I am a super rookie who really don't know much about MATLAB. However, my supervisor required me to rewrite a script to fit for our current study. I tried but I am not so sure how to do it, may I ask for some advice, please?
% calculate Stim accuracy
for a = 1:4 % total no. of conditions
StimACCC = mean(StimACC(Cond == a & Subject == i));
T3(:, a) = StimACCC; % save data
It works to generate the data that I need, however, in the second script that I am writing for there are multiple conditions, Cond (3) and Congruency (2).
I tried to write like below:
% calculate StimACC accuracy
for a = 1:3% total no. of conditions for Conditions
if Congruency > 0
StimACCC = mean(StimACC(CueCond == a & Congruency == 0 & Subject == i));
else
StimACCC = mean(StimACC(CueCond == a & Congruency == 1 & Subject == i));
T3(:, a) = StimACCC; % save data
It doesn't work and generate data as [0.979166666666667 1 NaN], however, after mixing Cond (3) and Congruency (2), there should be 6 data generated for each participant.
I don't know where the problem is and how to fix it. May I ask for some advice, please?

採用された回答

Ram Sreekar
Ram Sreekar 2023 年 6 月 29 日
Hi Karen,
As per my understanding, you want to generate a total of 6 outputs for each participant, depending on different values of the two variables Cond(3 values) and Congruency(2 values).
But according to your code, the result is being calculated depending on whether ‘Congruency’> 0 or not, for every ‘a’. So, only one result is generated for each value of ‘a’.
If you want to generate 6 different outputs for all combinations of values ‘Cond’ and ‘Congruency’, you can do it by using a nested for loop. You can refer to the example code given below.
% Create T3 with NaNs to store the 6 outputs
T3 = NaN(6, 1);
% index for T3
index = 1;
% calculate StimACC accuracy
for a = 1:3 % total no. of conditions for CueCond
for b = 0:1 % total no. of conditions for Congruency
StimACCC = mean(StimACC(CueCond == a & Congruency == b & Subject == i));
T3(index) = StimACCC;
index = index + 1;
end
end
Hope this helps you resolve your query.
  1 件のコメント
Karen
Karen 2023 年 6 月 29 日
Thank you for your detailed explantion, I can now understand why the if statement doesn't work. May I ask that if the index is used for calculation for each participants, please? (Sorry if I am asking a silly question as I just started learning how to 'read' the script)

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

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 6 月 29 日
1st of all, if you need to create if .. else .. end statement to compare scalars or signle value variables, for .. end loop is not necessary. If you want to check individual value of a vector or matrix within if .. else .. end condition, that is a different story.
See this DOC how to use if .. else .. end and when to use for .. end
  1 件のコメント
Karen
Karen 2023 年 6 月 29 日
Thank you for your help. I will read the DOC to see how and when to use if.

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

カテゴリ

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