How do I delete the first X number of rows in a matrix for each participant, where each participant's ID number is specified in one of the columns?

4 vues (au cours des 30 derniers jours)
Basically, I have a data set in MatLab, roughly 3000x4. One of the columns shows the participants name (they're numbered, so they'd be '1', '2', '3', etc.). Each participant has 84 data points (that is, they did the test 84 times, so we have data for each of the 84 trials). However, the first four times each participant played the game were practice trials, so I want to remove them. How would I code into Matlab to get rid of the first four trials of each participant?
For example, say I have the following matrix, where the first column is the participant's number:
1 2 5 3
1 4 5 2
1 5 2 3
1 5 2 4
2 6 7 9
2 6 7 8
2 6 7 2
2 4 2 6
3 8 0 3
3 2 6 1
3 1 6 3
3 9 2 5
How would I code it so that the first and second data point generated by each participant is removed? Instead I would have the following matrix:
1 5 2 3
1 5 2 4
2 6 7 2
2 4 2 6
3 1 6 3
3 9 2 5
This example above is a smaller version of what I'm trying to do.
So, the code would instruct the data to delete the first X number of rows per participant.
Thanks so much for the help in advance!
:)

Réponse acceptée

Voss
Voss le 19 Nov 2022
Here is one way:
data = [ ...
1 2 5 3; ...
1 4 5 2; ...
1 5 2 3; ...
1 5 2 4; ...
2 6 7 9; ...
2 6 7 8; ...
2 6 7 2; ...
2 4 2 6; ...
3 8 0 3; ...
3 2 6 1; ...
3 1 6 3; ...
3 9 2 5; ...
];
rows_to_delete = [];
[ids,~,jj] = unique(data(:,1));
for ii = 1:numel(ids)
idx = find(jj == ii);
rows_to_delete = [rows_to_delete; idx(1:min(numel(idx),2))];
end
data(rows_to_delete,:) = [];
disp(data)
1 5 2 3 1 5 2 4 2 6 7 2 2 4 2 6 3 1 6 3 3 9 2 5
That will remove the first up-to-2 rows of data for each ID (i.e., if there happen to be fewer than 3 rows for a given ID, all of them will be removed).
  2 commentaires
Billy
Billy le 19 Nov 2022
Thanks for this answer! I'm quite new to MatLab so just struggling a little bit to undersatnd what the different steps mean, and how to apply it to me 3000x4 array which has roughly 40 different participants, with (roughly) 80 data points each, where I want to remove the first four of each. The IDs start from '62' and go all the way to '102', with 80 data points each (again, roughly). Would this code above be applicable to such a big array, and if so, how?
Thank you so much for your help!
Best,
Billy
Voss
Voss le 20 Nov 2022
Yes, this will work for an array of that size.
To remove the first 4 rows for each participant instead of the first 2, change the 2 to a 4.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Just for fun dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by