How do I find the time between when the animal_speed exceeded the threshold till when it dipped below the threshold (all instances when this happened)?

The data set I am working with has 17 columns and over 10000 rows. Two particular columns are 'time' and 'animal_speed'. I am also using a Threshold of 0.10m/s.
To find all instances when the 'animal_speed' exceeds the Threshold, I use this:
>> [row, col, v] = find(data_set.animal_speed > 0.10);
which gives me all 'animal_speed' row numbers where the values are greater than 0.10.
Plotting the graph with 'time' and 'animal speed' and including the Threshold line (y=0.10) is not a problem.
However, how do I find the time between when the animal speed exceeded the threshold till when the speed dipped below the threshold (all instances when this happened)?
Thanks!

 Accepted Answer

There are likely other solutions, but this seems to work:
animal_speed = rand(1, 1E+4)/2; % Create Data
time = [0:1E+4]/350; % Create Data
Hspeed_log = animal_speed > 0.10; % Logical Index
Lspeed_log = ~Hspeed_log;
HspeedIdx = find(Hspeed_log);
LspeedIdx = find(Lspeed_log);
for k1 = 1:length(HspeedIdx)
IdxMtx(k1,:) = [HspeedIdx(k1) LspeedIdx(find(LspeedIdx > HspeedIdx(k1), 1, 'first'))];
end
[~,ia] = unique(IdxMtx(:,2)); % Indices Of Unique ‘LspeedIdx’ Values In ‘IdxMtx’
StartStopIdx = IdxMtx(ia,:); % Start-Stop Index Intervals
StartStopTimes = time(StartStopIdx); % Start-Stop Times For Speed > 0.10

2 Comments

Considering I've been using Matlab for barely a few weeks, I still deciphering your answer. =) (It's going to take me a while)
I did however plug your script in and it worked great! It's given me the start and stop times for when the animal_speed was greater than 0.10m/s.
Thank you very much!
My pleasure!
The documentation for the find function will explain how it works. The tilde (~) here means logical negation (in the ‘Lspeed_log’ assignment), and ‘don’t return this value to the workspace’ in the unique function call. The logical vectors will be (in this instance) equal to 1 when the animal is moving at greater than 0.1 m/s, and 0 otherwise.
Since you want the time the animal was traveling greater than 0.1 m/s, the first index will always be the beginning of that interval. The ‘IdxMtx’ loop takes that index value and then looks for the next value in the ‘LspeedIdx’ (low-speed index) that is greater.
A slightly different approach would have been to use the diff function on ‘Hspeed_log’, treating the values now as integers rather than logical values (automatic conversion in MATLAB), and looking for the transitions from 0 to 1 equaling 1, and from 1 to 0 equaling -1, then determining those positions in the vector using find. The rest of the code would have been much the same.
My code assumes that the animal has complete (start-stop) activity intervals during the recording, and isn’t moving faster than 0.1 m/s at the end of it. In that event, both times in the last interval will be the same. To prevent that, consider changing the loop to:
for k1 = 1:length(HspeedIdx)
NextIdx = find(LspeedIdx > HspeedIdx(k1), 1, 'first');
if isempty(NextIdx) % If Moving >0.1 m/s At End, Stop Loop
fprintf('\n\tIncomplete final interval.\n')
break
end
IdxMtx(k1,:) = [HspeedIdx(k1) LspeedIdx(NextIdx)];
end
This will break out of the loop if the last interval is incomplete, so all the intervals will be valid.

Sign in to comment.

More Answers (1)

That's certainly an erratic animal!!! :)
Anyway, this is the same question rephrased in slightly different terms just yesterday--
NB: the correction of using ";" for the concatenation operation since your data are also column-oriented. You simply need to turn your speed data into the 0-1 as the switch position the other poster already had; that's simply
speedFlag=speed>=threshold;
where speed is your data set column of interest and threshold the value. Choose '>' or '>=' at your preference of inclusive or no...

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!