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 views (last 30 days)
Show older comments
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!
:)
0 Comments
Accepted Answer
Voss
on 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)
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 Comments
Voss
on 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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!