How to create a for loop?

1 view (last 30 days)
Eveline Kallenberg
Eveline Kallenberg on 1 Feb 2020
Commented: Star Strider on 3 Feb 2020
Hi all,
I have this code beneath and I am wondering if and how I could put this in a for loop in order to only have a code of about 5 lines or so?
Thanks in advance!
%% 1. Data laden
load('PP1_TOJ.mat');
%% SOA -350ms
responsemin350=Data(Data(:,3)==-350,[3 6]);
xmin350=(responsemin350==80);
soundfirstmin350=sum(xmin350, 'all');
ymin350=(responsemin350==79);
lightfirstmin350=sum(ymin350, 'all');
soundfirstmin350(soundfirstmin350==0)=1;
lightfirstmin350(lightfirstmin350==0)=1;
proportionlightfirstmin350=lightfirstmin350/20
%% SOA -250ms
responsemin250=Data(Data(:,3)==-250,[3 6]);
xmin250=(responsemin250==80);
soundfirstmin250=sum(xmin250, 'all');
ymin250=(responsemin250==79);
lightfirstmin250=sum(ymin250, 'all');
soundfirstmin250(soundfirstmin250==0)=1;
lightfirstmin250(lightfirstmin250==0)=1;
proportionlightfirstmin250=lightfirstmin250/20
%% SOA -150ms
responsemin150=Data(Data(:,3)==-150,[3 6]);
xmin150=(responsemin150==80);
soundfirstmin150=sum(xmin150, 'all');
ymin150=(responsemin150==79);
lightfirstmin150=sum(ymin150, 'all');
soundfirstmin150(soundfirstmin150==0)=1;
lightfirstmin150(lightfirstmin150==0)=1;
proportionlightfirstmin150=lightfirstmin150/20
%% SOA -50ms
responsemin50=Data(Data(:,3)==-50,[3 6]);
xmin50=(responsemin50==80);
soundfirstmin50=sum(xmin50, 'all');
ymin50=(responsemin50==79);
lightfirstmin50=sum(ymin50, 'all');
soundfirstmin50(soundfirstmin50==0)=1;
lightfirstmin50(lightfirstmin50==0)=1;
proportionlightfirstmin50=lightfirstmin50/20
%% SOA 0ms
response0=Data(Data(:,3)==0,[3 6]);
x0=(response0==80);
soundfirst0=sum(x0, 'all');
y0=(response0==79);
lightfirst0=sum(y0, 'all');
soundfirst0(soundfirst0==0)=1;
lightfirst0(lightfirst0==0)=1;
proportionlightfirst0=lightfirst0/20
%% SOA +50ms
responseplus50=Data(Data(:,3)==50,[3 6]);
xplus50=(responseplus50==80);
soundfirstplus50=sum(xplus50, 'all');
yplus50=(responseplus50==79);
lightfirstplus50=sum(yplus50, 'all');
soundfirstplus50(soundfirstplus50==0)=1;
lightfirstplus50(lightfirstplus50==0)=1;
proportionlightfirstplus50=lightfirstplus50/20
%% SOA +150ms
responseplus150=Data(Data(:,3)==150,[3 6]);
xplus150=(responseplus150==80);
soundfirstplus150=sum(xmin150, 'all');
yplus150=(responseplus150==79);
lightfirstplus150=sum(yplus150, 'all');
soundfirstplus150(soundfirstplus150==0)=1;
lightfirstplus150(lightfirstplus150==0)=1;
proportionlightfirstplus150=lightfirstplus150/20
%% SOA +250ms
responseplus250=Data(Data(:,3)==250,[3 6]);
xplus250=(responseplus250==80);
soundfirstplus250=sum(xplus250, 'all');
yplus250=(responseplus250==79);
lightfirstplus250=sum(yplus250, 'all');
soundfirstplus250(soundfirstplus250==0)=1;
lightfirstplus250(lightfirstplus250==0)=1;
proportionlightfirstplus250=lightfirstplus250/20
%% SOA +350ms
responseplus350=Data(Data(:,3)==350,[3 6]);
xplus350=(responseplus350==80);
soundfirstplus350=sum(xplus350, 'all');
yplus350=(responseplus350==79);
lightfirstplus350=sum(yplus350, 'all');
soundfirstplus350(soundfirstplus350==0)=1;
lightfirstplus350(lightfirstplus350==0)=1;
proportionlightfirstplus350=lightfirstplus350/20

Accepted Answer

Star Strider
Star Strider on 1 Feb 2020
Try this:
D = load('PP1_TOJ.mat');
Data = D.Data;
V = [-350:100:150 -50:50:50 150:100:350];
for k = 1:numel(V)
responsemin{k,:} = Data(Data(:,3)==V(k),[3 6]);
soundfirstmin{k,:} = sum(responsemin{k}==80, 'all');
lightfirstmin{k,:}=sum(responsemin{k}==79, 'all');
soundfirstmin{k,:}(soundfirstmin{k}==0)=1;
lightfirstmin{k,:}(lightfirstmin{k}==0)=1;
proportionlightfirstmin{k,:}=lightfirstmin{k}/20;
end
The results are each (12x1) cell arrays. The ‘k’ subscripts correspond to the elements of ‘V’.
  6 Comments
Eveline Kallenberg
Eveline Kallenberg on 3 Feb 2020
Sorry, but I have very little experience in coding so I have no idea what you are saying haha sorry! Could you maybe give an example with code?
Rik
Rik on 3 Feb 2020
soundfirst{k,:}=sum(response{k}==80, 'all');
% ^ ^
% not (k)
%and before your loop:
response={};
lightfirst={};
soundfirst={};

Sign in to comment.

More Answers (1)

Eveline Kallenberg
Eveline Kallenberg on 3 Feb 2020
Thanks everyone for your time to try to help me! Ultimately, I figured it out and my code can be found below.
%% 1. Loading data
D=load('PP1_TOJ.mat');
Data=D.Data;
%% 2. For loop with SOA's
SOAs = [-350 -250 -150 -50 0 50 150 250 350];
for k = 1:numel(SOAs)
response(:,:)=Data(Data(:,3)==SOAs(k),[3 6]);
soundfirst(k)=sum(response(:,2)==80, 'all');
lightfirst(k)=sum(response(:,2)==79, 'all');
proportionlightfirst(k)=lightfirst(k)/20;
end
  3 Comments
Eveline Kallenberg
Eveline Kallenberg on 3 Feb 2020
Yes almost, so thanks to your help I managed to work it out! :D
Star Strider
Star Strider on 3 Feb 2020
As always, my pleasure!

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!