フィルターのクリア

Averaging across labels in a time-series data

3 ビュー (過去 30 日間)
Shreshtha Chaturvedi
Shreshtha Chaturvedi 2024 年 5 月 17 日
コメント済み: Siddharth Bhutiya 2024 年 5 月 20 日
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'})
data_1 = 10x2 table
time stage ____ _____ 5 {'A'} 5 {'B'} 5 {'B'} 5 {'B'} 5 {'A'} 5 {'A'} 5 {'C'} 5 {'C'} 5 {'C'} 5 {'B'}
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'})
data2 = 10x2 table
time stage ____ _____ 5 {'A'} 5 {'A'} 5 {'C'} 5 {'C'} 5 {'C'} 5 {'A'} 5 {'C'} 5 {'C'} 5 {'B'} 5 {'C'}
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
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)
data = 10x1 timetable
Time Stage _____ _____ 5 sec A 5 sec B 5 sec B 5 sec B 5 sec B 5 sec A 5 sec A 5 sec A 5 sec A 5 sec C
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 ExchangeTime Series についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by