Grouping consectuve datapoints from a table into an array

1 Ansicht (letzte 30 Tage)
Youssif Youssif
Youssif Youssif am 3 Nov. 2022
Beantwortet: Lei Hou am 18 Nov. 2022
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).

Akzeptierte Antwort

Lei Hou
Lei Hou am 18 Nov. 2022
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]}

Weitere Antworten (1)

David Hill
David Hill am 3 Nov. 2022
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 Kommentar
Youssif Youssif
Youssif Youssif am 3 Nov. 2022
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by