필터 지우기
필터 지우기

Append values of nested loop into matrix

조회 수: 1 (최근 30일)
MadjeKoe
MadjeKoe 2022년 6월 14일
댓글: MadjeKoe 2022년 6월 15일
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