Averaging across labels in a time-series data
3 次查看(过去 30 天)
显示 更早的评论
Hi, I have a time series data that loos like this:
time = repmat(5, 10, 1);
stages = {'A', 'B', 'C'};
stage = stages(randi(numel(stages), 10, 1))';
stage = cellstr(stage);
data_1 = table(time, stage, 'VariableNames', {'time', 'stage'})
If I have another time series data,
stages = {'A', 'B', 'C'};
stage = stages(randi(numel(stages), 10, 1))';
stage = cellstr(stage);
data2 = table(time, stage, 'VariableNames', {'time', 'stage'})
I wanted to know if there's a simple way to get a final table (data) after taking an average of the stages at each duration timepoint. I thought of assigning the three stages A, B, and C integer values and then taking the average at each time point. The round off value that is the closest to the integer is assigned the same stage. For example, A = 0, B =1, C = 2. In the MWE above, at the first time point, we have the average = 0 so "data" will have A as the first stage.
I was wondering if there is a neater way to go about this. Or if there's an inbuilt matlab tool that does this for us. Thank you!
1 个评论
Siddharth Bhutiya
2024-5-20
First a couple of general suggestions, for text data you should consider using strings over cellstrs. In your case these are labels with a fixed set of values so a much better option would be to make these categoricals. Second, you mentioned that this is timeseries data, so I would suggest using timetables over tables to represent this data, as they offer a lot of functionality specific to timeseries analysis. So assuming time of 5 here means 5 seconds I would use the following timetable:
time = seconds(repmat(5, 10, 1));
stages = ["A";"B";"C"];
stage = categorical(stages(randi(numel(stages), 10, 1)));
data = timetable(stage,VariableNames="Stage",RowTimes=time)
If you are doing timeseries analysis this would make your life a lot easier. Now going back to your question, since you are trying to take average of labels, I'm afraid there isnt really a inbuilt way to do, because there is no standard definition of what average of text labels mean. So you would have to come up with your own definition of the average, like you did by converting those to integers and assigning the nearest label after taking the average.
Depending on how heavy handed you want to go, you could consider converting your stage into a user-defined MATLAB class and then define the mean method for it to do exactly what you are doing, but given your example, that seems like an overkill.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!