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

1 Ansicht (letzte 30 Tage)
lil brain
lil brain am 14 Mär. 2022
Kommentiert: Voss am 15 Mär. 2022
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?

Akzeptierte Antwort

Voss
Voss am 14 Mär. 2022
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 Kommentare
Voss
Voss am 15 Mär. 2022
@little brain You're welcome! If that answers your question, please mark my answer as 'Accepted'. I appreciate it!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by