# Using a loop to add to dataset without overwriting previous calculations

2 Ansichten (letzte 30 Tage)
Erik J am 27 Jun. 2017
Kommentiert: Erik J am 27 Jun. 2017
I have a code which makes a number of calculations based on a set of experimental data. The output is currently several matrices, each for one of 5 experimental conditions, and each of which provides the data for that condition (e.g. Subject Number, VariableX, VariableY, etc).
General issue: I need to calculate these matrices for 50 subjects to create one big data set for each condition. To do this, I made a loop of the code. My problem is that the code then just now returns the data for the last subject. E.g., it writes over the data for the previous subject on each loop. I would like to fix it so that on each loop it just adds the data for that subject to the matrix of data for that condition. I don't know how to do this.
The further problem is that there are not an equal number of data points for each subject, so concatenating the matrix on each loop is probably not possible? I think I need to use accumarray or something similar? Any thoughts or ideas would be very helpful - I am not too experienced at this.
The code is long and messy, but here is a shortened version with the crucial elements:
subjStart = 1;
subjFinish = 54;
nSubj = 54;
for gg = subjStart:subjFinish
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Code to calculate all the variables used in the following data tables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Create Data Tables
yy = (ones([1,length(SIcon1)])) .* gg; %vector of subject number
yy = yy'; %make column
dd = (ones([1,length(SIcon1)])) .* dist; %vector of distance criterion
dd = dd'; %make column
cc = (ones([1,length(SIcon1)])); %condition
cc = cc'; %make column
SubjDataCon1 = [yy dd cc SIcon1 SurveyNumCon1]; %data for condition 1 for each survey
yy = (ones([1,length(SIcon2)])) .* gg; %vector of subject number
yy = yy'; %make column
dd = (ones([1,length(SIcon2)])) .* dist; %vector of distance criterion
dd = dd'; %make column
cc = (ones([1,length(SIcon2)])) .*2; %condition
cc = cc'; %make column
SubjDataCon2 = [yy dd cc SIcon2 SurveyNumCon2]; %data for condition 2 for each survey
yy = (ones([1,length(SIcon3)])) .* gg; %vector of subject number
yy = yy'; %make column
dd = (ones([1,length(SIcon3)])) .* dist; %vector of distance criterion
dd = dd'; %make column
cc = (ones([1,length(SIcon3)])) .*3; %condition
cc = cc'; %make column
SubjDataCon3 = [yy dd cc SIcon3 SurveyNumCon3]; %data for condition 3 for each survey
yy = (ones([1,length(SIcon4)])) .* gg; %vector of subject number
yy = yy'; %make column
dd = (ones([1,length(SIcon4)])) .* dist; %vector of distance criterion
dd = dd'; %make column
cc = (ones([1,length(SIcon4)])) .*4; %condition
cc = cc'; %make column
SubjDataCon4 = [yy dd cc SIcon4 SurveyNumCon4]; %data for condition 4 for each survey
yy = (ones([1,length(SIcon56)])) .* gg; %vector of subject number
yy = yy'; %make column
dd = (ones([1,length(SIcon56)])) .* dist; %vector of distance criterion
dd = dd'; %make column
cc = (ones([1,length(SIcon56)])) .*56; %condition
cc = cc'; %make column
SubjDataCon56 = [yy dd cc SIcon56 SurveyNumCon56]; %data for condition 56 for each survey
clear yy;
clear dd;
clear cc;
clear SIcon1;
clear SIcon2;
clear SIcon3;
clear SIcon4;
clear SIcon56;
clear SurveyNumCon1;
clear SurveyNumCon2;
clear SurveyNumCon3;
clear SurveyNumCon4;
clear SurveyNumCon56;
end
##### 4 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Jan am 27 Jun. 2017
Bearbeitet: Jan am 27 Jun. 2017
Replace
yy = (ones([1,length(SIcon1)])) .* gg; %vector of subject number
yy = yy'; %make column
by
yy = repmat(gg, length(SIcon1), 1);
and so on for all created variables. Then omit the pile of useless clear commands at the end. Finally your code will be much leaner and faster.
Erik J am 27 Jun. 2017
Thanks!

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Logical finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by