Indexing array by 2 variables in a for loop

4 visualizaciones (últimos 30 días)
Paquerette
Paquerette el 7 de Feb. de 2024
Comentada: MarKf el 12 de Feb. de 2024
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

Respuestas (1)

MarKf
MarKf el 7 de Feb. de 2024
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 comentarios
Paquerette
Paquerette el 8 de Feb. de 2024
Thank you. I'll have a look and see if this does the trick.
In the meantime I had done the code below where I reshaped the 2 51x20 matrices that were created in a nested for loop into a 51x5x4 tensor and this works.
However, my supervisor would like me to skip the intermediate step of creating the 2D matrix and create the tensor directly and again I am stumped.
% Loop over the columns of the 2D matrix and fill the 4D matrix
for i = 1:5:20
% Calculate the index for the 4D matrix
idx = (i + 4) / 5;
% Extract the block of 5 columns from the 2D matrix
blockVE = matrixLangVE(:, i:i+4);
blockPE = matrixLangPE(:, i:i+4);
% Assign the block to the corresponding slice in the 4D matrix
arrayLangVE(:, :, idx) = blockVE;
arrayLangPE(:, :, idx) = blockPE;
%end
MarKf
MarKf el 12 de Feb. de 2024
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by