Append values of nested loop into matrix

3 次查看(过去 30 天)
Hi guys, I am trying to make a nested loop work and append the values to a existing matrix. I have 28 participants (who have numbers varying from 1 - 56), that each performed 5 blocks of 120 trials. For each individual block, I want to accumulate the correct responses (resp_jong == 1) at each trial (so up until the 120th trial) and append it 1 column & I want another column where I accumulate the trial that were responded to (resp_jong == -1 | resp_jong == 1). I have been working on the script all day, but I am completely lost and cannot get it to work at all. Does there happen to be anyone that can help me out a little bit? Thank you very very much in advance
r=readmatrix('data_jong_edit.txt');
r(isnan(r(:,1)),:)=[];
empty = nan(16560,2);
r = [r empty];
% M(end+1,:) = NaN;
% corrects = [];
% responses = [];
count1 = 0;
count2 = 0;
re = r(:,9);
for k=1:length(tr)
if re(k,:) == 1
count1 = count1 + 1;
count2 = count2 + 1;
cor(k,:) = count1;
res(k,:) = count2;
elseif re(k,:) == -1
count1 = count1;
count2 = count2 + 1;
cor(k,:) = count1;
res(k,:) = count2;
elseif re(k,:) == 9999
count1 = count1;
count2 = count2;
cor(k,:) = count1;
res(k,:) = count2;
end
end

回答(1 个)

David Hill
David Hill 2022-6-14
r=readmatrix('data_jong_edit.txt');
r(isnan(r(:,1)),:)=[];
u=unique(r(:,1));
for k=1:length(u)
re=r(r(:,1)==u(k),:);
re=re';
try
rs=reshape(re,11,120,[]);
s1(:,k)=squeeze(sum(rs(9,:,:)==1));%number of 1 responses per block
s2(:,k)=squeeze(sum(rs(9,:,:)==-1));%number of -1 responses per block (s1+s2 is number of 1 and -1)
catch
continue;
end
end
  2 个评论
MadjeKoe
MadjeKoe 2022-6-15
Thank you so much for your help!! I actually meant to store the values for each single trial, do you maybe happen to know how I can store these so that the total score is displayed at the moment of the trial? See the picture for an example.
MadjeKoe
MadjeKoe 2022-6-15
I updated my script and it does the job, except i cannot get it to loop every 120 trials

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by