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
댓글 수: 0
답변 (1개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!