How to include values starting from the nth instance of a value?

Hi,
I have a data set where column 4 indicates a type of event. Columns 7-27 in this same data set are the raw data I am working with. I want to write two pieces of code where I first, include all the raw data (columns 7-27) until the a certain event occurs for the first time (column 4), and second, include all the raw data (columns 7-27) after the event occurs for the last time (column 4).
What I have:
Here I want to include all the raw data from the lines where column 4 equals "Basket Glitch".
baskets_data_participant = data_in(data_in(:,4) == "Basket Glitch", 7:end);
What I need:
1) I want to change the above code to include all the raw data up to the first instance of "Basket Glitch".
2) I want to change the above code to include all the raw data after the last instance of "Basket Glitch"
How would I go about doing that?

 Accepted Answer

bg_idx = find(strcmp(data_in(:,4),'Basket Glitch'));
% include all the raw data up to the first instance of "Basket Glitch":
data_out = data_in(1:bg_idx(1),7:end);
% include all the raw data after the last instance of "Basket Glitch":
data_out = data_in(bg_idx(end)+1:end,7:end);
Of course, you have to decide what to do when there are no instances of "Basket Glitch" in column 4.

6 Comments

Thank you for the quick response. May I ask you a folow up question?
I have used your code above and put it into a for loop like so:
basket_glitch_idx = find(strcmp(data_in(:,4),'Basket Glitch'));
pre_basket_data_participant = data_in(1:basket_glitch_idx(1),7:end);
However, I am getting this error
Index in position 1 exceeds array bounds (must not exceed 23925).
Error in Before_and_after_comparison (line 31)
pre_balls_data_participant = data_in(1:balls_glitch_idx(1),7:end);
Any idea why that is?
Oh I think this happens because "Basket Glitch" it doesnt appear in data_in correct?
Well, the error is referring to "balls_glitch_idx" rather than "basket_glitch_idx".
But assuming balls_glitch_idx is defined as:
balls_glitch_idx = find(strcmp(data_in(:,4),'Balls Glitch'));
then my guess would be that the number of rows of data_in decreases at some point, after balls_glitch_idx is calculated and before the error line:
pre_balls_data_participant = data_in(1:balls_glitch_idx(1),7:end);
so that balls_glitch_idx(1) is greater than the number of rows of data_in, causing the error.
If the error was because "Basket Glitch" (or "Balls Glitch" or whatever) did not appear in data_in, then the error would say: "Index in position 1 exceeds array bounds (must not exceed 0)" The difference being the stated size of the array being indexed.
If the relevant "Glitch" string doesn't appear then the idx variable would be empty (size 0), so you'd get the error by doing idx(1). Instead, it seems the error is with indexing data_in (which apparently has 23925 rows) using data_in(1:idx(1), ...).
@little brain You're welcome! If that answers your question, please mark my answer as 'Accepted'. I appreciate it!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 14 Mar 2022

Commented:

on 15 Mar 2022

Community Treasure Hunt

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

Start Hunting!