How to create a for loop?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Eveline Kallenberg
le 1 Fév 2020
Commenté : Star Strider
le 3 Fév 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
0 commentaires
Réponse acceptée
Star Strider
le 1 Fév 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 commentaires
Rik
le 3 Fév 2020
soundfirst{k,:}=sum(response{k}==80, 'all');
% ^ ^
% not (k)
%and before your loop:
response={};
lightfirst={};
soundfirst={};
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!