Grouping consectuve datapoints from a table into an array

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by