Grouping consectuve datapoints from a table into an array

1 回表示 (過去 30 日間)
Youssif Youssif
Youssif Youssif 2022 年 11 月 3 日
回答済み: Lei Hou 2022 年 11 月 18 日
Hello,
I have atatched a picture of my 54x2 table. I was looking for help with my variable InstantPowerX. Basically, I would like a code that would organize consective numbers into an array called pulses. (For example, the first three point 20327,20328,20329 are consectuive, and they will be called pulse 1, 22516,22517,22518 are the next consectuive dataset and they will be stored in array called pulse 2 and so on) There should be a total of 18 pulses *18x3=54 since all data is 3 seconds in length before jumping to the next three seconds).

採用された回答

Lei Hou
Lei Hou 2022 年 11 月 18 日
Hi Youssif,
Try the following code to see whether it works for you. The main idea is to use diff to find the group edge, and then use groupsummary to group row using the group edge information.
>> t = table([20327;20328;20329;22516;22517;22518;22519;25832;25833;28979],(1:10)','VariableNames',["InstantPowerX" "InstantPowerY"])
t =
10×2 table
InstantPowerX InstantPowerY
_____________ _____________
20327 1
20328 2
20329 3
22516 4
22517 5
22518 6
22519 7
25832 8
25833 9
28979 10
>> groupEdgeRowInex = [true; (diff(t.InstantPowerX) ~= 1)];
>> tOut = groupsummary(t,'InstantPowerX',[t.InstantPowerX(groupEdgeRowInex);t.InstantPowerX(end)],@(x){x})
tOut =
4×3 table
disc_InstantPowerX GroupCount fun1_InstantPowerY
__________________ __________ __________________
[20327, 22516) 3 {3×1 double}
[22516, 25832) 4 {4×1 double}
[25832, 28979) 2 {2×1 double}
[28979, 28979] 1 {[ 10]}

その他の回答 (1 件)

David Hill
David Hill 2022 年 11 月 3 日
Best to keep all the data together. Just reshape the array and index into it.
pulse=reshape(yourTable.InstantPowerX,3,[])';%pulse1 would equal pulse(1,:), pulse2 would equal pulse(2,:)
  1 件のコメント
Youssif Youssif
Youssif Youssif 2022 年 11 月 3 日
is there a more general approach in cases were consecutve data is not always 3 seconds in length, but can be a range, example, data that is between 9 and 11 seconds can be grouoped into pules instead

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by