Smallest interval in array

3 views (last 30 days)
Pesach Nestlebaum
Pesach Nestlebaum on 27 Apr 2022
Commented: Star Strider on 29 Apr 2022
Hi everyone,
I am attempting to write software that organizes data from a machine that gets triggered multiple times throughout the day. I need to find which times in the day have the highest rate of triggering. In other words, how do I find the intervals with the smallest gaps in time within the data list?
The .xlsx list is attached.
Each row on the list represents an "encounter", and the machine takes a reading of the current time, temp, weight and light level. I need to find out what time interval has the highest rate of triggers.
Any ideas where to start?
Thanks in advance!
  2 Comments
Jan
Jan on 27 Apr 2022
Start with importing the Excel file to Matlab. Which Matlab version are you using?
Then define, what you exactly want to solve: Are you really looking for the shortest distance between the time values in the 1st column? Or does "interval with highest rate" mean e.g. a certain interval of e.g. 2 hours, which contains the highest number of events? Do the numbers in the other columns matter?
Pesach Nestlebaum
Pesach Nestlebaum on 27 Apr 2022
Thanks for answering,
I imported the data already and separated the other columns for later use. That's a great clarification, I guess I am looking for a two hour interval with the highest rate of events.
Ultimately, i'd like the software to determine which times of the day are the most active for any set of data it receives, not this specific list, and keep a log for a monthly average. But this is a good start.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 27 Apr 2022
I am not certain what the desired result is here.
Try this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/980625/enc_data_samp.xlsx', 'VariableNamingRule','preserve')
T1 = 59×4 table
Time Light Weight Temp ________ _____ ______ ____ 0.04375 1 450 52 0.069444 0 988 52 0.091667 1 633 53 0.14722 1 68 54 0.16667 2 77 55 0.20833 2 540 58 0.22431 4 406 57 0.23681 4 105 58 0.24306 4 100 60 0.25 5 46 62 0.27014 6 79 63 0.29375 7 102 63 0.29583 7 343 63 0.30208 7 76 65 0.30417 7 89 65 0.30486 7 68 65
Tt = cumsum([0; T1.Time]); % Create Time Vector
Td = diff([0; 0; T1.Time])+eps; % Differences Between Trigger Events
Fr = 1./Td; % Frequency Of Trigger Events
figure
plot(Tt, Td)
grid
xlabel('Time')
ylabel('Trigger Frequency')
I am not certain what the time vector (the independent variable here) actually is (I suspect that it should be ‘time of day’ or something similar), however this is likely the easiest way to determine the frequency of the trigger events as a function of it.
.
  8 Comments
Pesach Nestlebaum
Pesach Nestlebaum on 29 Apr 2022
One last question:
This code produces a variable "idx" which is equal to a single number: 8. This seems to be because there is only one column of time data being processed.
Suppose there were 7 columns of time entries instead of just one, how would i produce an array with all 7 idx values? In other words, say there were 7 days of data, each day had different number of triggers. Can I accomplish this with a for loop? Obviously I need it to be coded, not manually updating the list every time.
Thanks!
Star Strider
Star Strider on 29 Apr 2022
As always, my pleasure!
The ‘idx’ variable is simply the row number of the ‘Trigger’ tally maximum. It can be used to refer to the entire row of the timetable, whatever the timetable contains, so:
maxTriggerRow = TT1c{idx,:}
to display all the variables in that row, or equivalently:
maxTriggerRow = TT1c(idx,:)
to display them with their variable names as well (note the difference between the curly braces {} and parentheses ()).
Here, it just happened to also be equivalent to the hour, and the fprintf statement referred to it as such.

Sign in to comment.

More Answers (1)

Thomas Pursche
Thomas Pursche on 27 Apr 2022
Hello Pesach,
when I understood it correctly please find in the following a first approach. Just read in the table, get the specific column and afterwards calculate the distances and minimal entry. Afterwards you can put them into an interval and count them if you want to.
% read in the table
tableData = readtable('enc_data_samp.xlsx');
% access intersting column
timeColumn = tableData.Time;
% calculate distance between each entry
for ii=1:numel(timeColumn)-1
distanceList(ii) = timeColumn(ii+1)-timeColumn(ii);
end
% get the minimal distance
minimalDistance = min(distanceList);
Best regards,
Thomas
  1 Comment
Pesach Nestlebaum
Pesach Nestlebaum on 27 Apr 2022
Thank you! This is what I needed in terms of finding all the distances between each entry. how can I now fin which hour has the most entries aka the shortest overall distance if all the distances within that hour are summed up?

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!