Indexing array by 2 variables in a for loop
8 次查看(过去 30 天)
显示 更早的评论
Hello
I have 2 51x20 matrices with data for 4 participants over 5 time periods. It is currently organised by time period in that colums 1-4 are for duration 1 for each participant, colums 5-8 for duration 2 for each participant, and so forth.
I would like to access this data for a t test permutation so need to create a nested for loop first by duration, then by participant (this can be reversed) in order to run the permutation for the 20 diffferent data points. I'm having difficulty figuring out how to access the correct column of data in the above matrix
I have variables set up for time periods and participant number as such. I thought about creating indiviual matrices by participant (and actually have) but think this isn't the best route as it's very messy.
Very new to matlab and programming in general, I would greatly appreciate input on how to best organise/access the information I need in the matrices.
ssSel = [102 104 105 106]; % Selected participants
stimDurSel = [1 2 3 4 5]; % Stimulus lengths
% Observed t-test need to take data from results table by stim length
for stimDur = stimDurSel
for ss = ssSel
0 个评论
回答(1 个)
MarKf
2024-2-7
You need to select each participant for each period, then doing a ttest on only those 51(*2 data matrices possibly) datapoints, then? Not 100% clear if that's the case, but if it's just data section anyway (and not homework) you can wind up something quickly like this (and can leave the data structure as is) and change it to your needs:
ndat = 51; nsubjs = 4; ndurs = 5; tspoints = nsubjs*ndurs;
fakedata = reshape(1:tspoints*ndat,ndat, tspoints);
% preallocate subjsdata ttestres durgroup here in case (like ttestres = nan(ndurs,nsubjs); see below)
for idur = 1:ndurs
tsel = (idur-1)*nsubjs+1:idur*nsubjs; %1:4, 45:8 etc
durgroup = fakedata(:,tsel); %51x4 duration groups; can do durgroup{idur} = to store for later
% either do that or a durgroup2 = data2(:,tsel); too from the other matrix
for isub = 1:nsubjs
subjdat = durgroup(:,isub); %51 single participant coloumn vector
% subjsdata{isub}(:,idur) indiviual matrices by participant
% subjdat2 = durgroup2(:,isub) from the other matrix
% ttestres(idur,isub) = ttest(subjdat,subjdat2) to fix, but this is unikely what you want to do actually
end
break
end
2 个评论
MarKf
2024-2-12
Sorry for the late reply, but it's not often that there is a follow up (usually a different issue is brought up in a different question/thread, but it's better this way since this is the same learning to code problem).
Again it's not very clear what is that you need, I can extrapolate some because I'm assuming it's some kinda beginning PhD science (neuro maybe), but I can't be completely sure. By tensor, do you just mean a 3D matrix or is it actually somthing to do with shape/direction? It does not matter anyway.
The again if the issue it's just skipping that step to avoid creating useless variables:
% Extract the block of 5 columns from the 2D matrix
% blockVE = ;
% blockPE = ;
% Assign the block to the corresponding slice in the 4D matrix
arrayLangVE(:, :, idx) = matrixLangVE(:, i:i+4);
arrayLangPE(:, :, idx) = matrixLangPE(:, i:i+4);
You see where my perplexity comes from, not sure this is what you were asking. Still, consider accepting the answer if that's what you were looking for or if it helped.
另请参阅
类别
在 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!