select corresponding values out of different columns of a matrix

8 visualizzazioni (ultimi 30 giorni)
Hi, We have a matrix (480 x 23) and we need to extract data from it. For example: in column 5 there are the numbers of 1 to 6 and we need to extract the numbers 1. At the same we need the corresponding reaction times of these values which can be found in column 14. Thus far, we have
[n,m]=size(trialList);
for k=1:n;
r = trialList(k,5);
if r == 1
disp ('trailList14');
end
end
We are able to find the 80 rows where number 1 is mentioned. But instead showing the 80 corresponding reaction times, Matlab shows 80 times the word 'triallist14'.
How will we be able to select the corresponding reaction times with the corresponding values of column 5?

Risposta accettata

Birdman
Birdman il 3 Apr 2018
Modificato: Birdman il 3 Apr 2018
vals=trialList(find(trialList(:,5)==1),[5 14]);
  6 Commenti

Accedi per commentare.

Più risposte (1)

Dotje123
Dotje123 il 4 Mag 2018
We were very content with the last solution you gave us. Now we are stuck again. We test more and more participants and every time we have to make new loops for a single participant. We were wondering if there is a way to get the output for all participants separately and only using one set of loops. thusfar, we have loaded a map with all the files of the participants.
load(['\\map...s' num2str(subjectNums(s)) '_2.mat'])
for vals1_1_5 = trialList(trialList(:,5)==1 & trialList(:,19)==5,14);
median1_1_5 = nanmedian(vals1_1_5)
end
for vals1_2_5 = trialList(trialList(:,5)==2 & trialList(:,19)==5,14);
median1_2_5 = nanmedian(vals1_2_5)
end
for vals1_3_5 = trialList(trialList(:,5)==3 & trialList(:,19)==5,14);
median1_3_5 = nanmedian(vals1_3_5)
end
for vals1_4_5 = trialList(trialList(:,5)==4 & trialList(:,19)==5,14);
median1_4_5 = nanmedian(vals1_4_5)
end
for vals1_5_5 = trialList(trialList(:,5)==5 & trialList(:,19)==5,14);
median1_5_5 = nanmedian(vals1_5_5)
end
for vals1_6_5 = trialList(trialList(:,5)==6 & trialList(:,19)==5,14);
median1_6_5 = nanmedian(vals1_6_5)
end
participant1_5 = [median1_1_5 median1_2_5 median1_3_5 median1_4_5 median1_5_5 median1_6_5]
%plot (overalmedian1, overalmedian2, overalmedian3, overalmedian4,
%overalmedian5, overalmedian6, overalmedian7, overalmedian8, overalmedian9,
%overalmedian10)
load('s2_2');
for vals2_1_5 = trialList(trialList(:,5)==1 & trialList(:,19)==5,14);
median2_1_5 = nanmedian(vals2_1_5)
end
for vals2_2_5 = trialList(trialList(:,5)==2 & trialList(:,19)==5,14);
median2_2_5 = nanmedian(vals2_2_5)
end
for vals2_3_5 = trialList(trialList(:,5)==3 & trialList(:,19)==5,14);
median2_3_5 = nanmedian(vals2_3_5)
end
for vals2_4_5 = trialList(trialList(:,5)==4 & trialList(:,19)==5,14);
median2_4_5 = nanmedian(vals2_4_5)
end
for vals2_5_5 = trialList(trialList(:,5)==5 & trialList(:,19)==5,14);
median2_5_5 = nanmedian(vals2_5_5)
end
for vals2_6_5 = trialList(trialList(:,5)==6 & trialList(:,19)==5,14);
median2_6_5 = nanmedian(vals2_6_5)
end
participant2_5 = [median2_1_5 median2_2_5 median2_3_5 median2_4_5 median2_5_5 median2_6_5]
load('s3_2');
for vals3_1_5 = trialList(trialList(:,5)==1 & trialList(:,19)==5,14);
median3_1_5 = nanmedian(vals3_1_5)
end
for vals3_2_5 = trialList(trialList(:,5)==2 & trialList(:,19)==5,14);
median3_2_5 = nanmedian(vals3_2_5)
end
for vals3_3_5 = trialList(trialList(:,5)==3 & trialList(:,19)==5,14);
median3_3_5 = nanmedian(vals3_3_5)
end
for vals3_4_5 = trialList(trialList(:,5)==4 & trialList(:,19)==5,14);
median3_4_5 = nanmedian(vals3_4_5)
end
for vals3_5_5 = trialList(trialList(:,5)==5 & trialList(:,19)==5,14);
median3_5_5 = nanmedian(vals3_5_5)
end
for vals3_6_5 = trialList(trialList(:,5)==6 & trialList(:,19)==5,14);
median3_6_5 = nanmedian(vals3_6_5)
end
participant3_5 = [median3_1_5 median3_2_5 median3_3_5 median3_4_5 median3_5_5 median3_6_5]
To be clear, we now make a selection per participant for every condition. What we want, is to be able to load all the participants into one set of loops (shown below), and get an output of all the medians per participant.
for vals1_1_5 = trialList(trialList(:,5)==1 & trialList(:,19)==5,14);
median1_1_5 = nanmedian(vals1_1_5)
end
for vals1_2_5 = trialList(trialList(:,5)==2 & trialList(:,19)==5,14);
median1_2_5 = nanmedian(vals1_2_5)
end
for vals1_3_5 = trialList(trialList(:,5)==3 & trialList(:,19)==5,14);
median1_3_5 = nanmedian(vals1_3_5)
end
for vals1_4_5 = trialList(trialList(:,5)==4 & trialList(:,19)==5,14);
median1_4_5 = nanmedian(vals1_4_5)
end
for vals1_5_5 = trialList(trialList(:,5)==5 & trialList(:,19)==5,14);
median1_5_5 = nanmedian(vals1_5_5)
end
for vals1_6_5 = trialList(trialList(:,5)==6 & trialList(:,19)==5,14);
median1_6_5 = nanmedian(vals1_6_5)
end
participant1_5 = [median1_1_5 median1_2_5 median1_3_5 median1_4_5 median1_5_5 median1_6_5]
Thank you in advance
  3 Commenti
Stephen23
Stephen23 il 4 Mag 2018
@Julia Verhaegh: The problem is having lots of numbered variables, and then trying to access them. You would be much better of keeping your data together as much as possible, using vectors/matrices/arrays. When beginners use lots of numbered/sequential variable names then they force themselves into either copy-and-pasting code (huge waste of your time) or writing slow, complex, buggy code (huge waste of your computer's time). Read this to know more:
Once you keep your data in just a few arrays then accessing it is trivial using indexing. Indexing is neat, very simple, and extremely efficient. It avoids all of the problems with numbered variables (which are just de-facto indices, so why not turn them into real indices?).
Razvan Carbunescu
Razvan Carbunescu il 4 Mag 2018
While there might be a few more aspects to your question it seems like you are trying to compute medians for groups of values.
In R2018a you can accomplish this easier by keeping the data together and using groupsummary.
If using an earlier release can use the findgroups / splitapply model to achieve the same result.

Accedi per commentare.

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by