MATLAB Answers

How to relate two data columns in a data table.

21 views (last 30 days)
Kirsty McCusker
Kirsty McCusker on 10 Feb 2020
Commented: Rajani Mishra on 18 Feb 2020
I have a data table that expresses how many times a certain thing happens.
eg,
0 appears 3 times
1 appears 4 times etc.
The time these numbers appear is related to another column and lasts for a certain amount of time. I was wondering if there is a way I can relate these 2 columns so I can get an output of something similar to this:
0 appears 3 times for 4 seconds
1 appears 4 times for 10 seconds.
The code I am currently using is
[u, ~, uidx] = unique(A(:));
counts = accumarray(uidx, 1);
fprintf('%d appears %d times\n', [u, counts].');
Thanks.

  0 Comments

Sign in to comment.

Answers (1)

Rajani Mishra
Rajani Mishra on 13 Feb 2020
I am not sure about the structure of the data table, but for finding the amount of time a certain thing lasts (considering that value is present in a column of the table) you can try using find() function on the column passing the respective thing as an input arguement.
Please find more information about find() in the below link :

  4 Comments

Show 1 older comment
Rajani Mishra
Rajani Mishra on 18 Feb 2020
can you provide your data table? because from the excel sheet's GPS time I am not able to understand whether its the time that certain thing appeared or something else, also can you explain more about your question
Kirsty McCusker
Kirsty McCusker on 18 Feb 2020
I am not sure how to attach the data table but I have changed the data I am going to use. Basically each count occurs at a certain time and I am looking to get a script which will tel me:
a) how many times each mode occurs i.e, 18 occurs 5 times
b) how long this occurance lasts, i.e 18 occurs 5 times for 5 seconds
I have a script which tells me how many times each mode occurs so I am just looking for an example to relate the two data columns so I can get b) to happpen as well.
Rajani Mishra
Rajani Mishra on 18 Feb 2020
From what I have understood is that you trying to calculate the duration every mode lasts from the GPS time of mode occurences. The minimum GPS time for a mode is considered the starting point for time calculation and maximum is the ending point.
From my understanding I have written below code taking 18 as mode for example, you can try below code for every mode.
Note : I have renamed your 'Example Results.xlsx' as 'ExampleResults.xlsx'
T = readtable('ExampleResults.xlsx');
time = T{:,1};
% time has the GPS time of all modes
mode = T{:,2};
% mode has all the mode values
% For a specific mode I have tried below script to get the duration it lasts for
indices = find(mode == 32);
% Find all the indices the mode occures
modeTimes = time(indices);
% Get all the GPS times
dur = max(modeTimes) - min(modeTimes);
% dur will have the time duration a mode lasts for

Sign in to comment.

Sign in to answer this question.