Grouping consectuve datapoints from a table into an array

1 visualización (últimos 30 días)
Youssif Youssif
Youssif Youssif el 3 de Nov. de 2022
Respondida: Lei Hou el 18 de Nov. de 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).

Respuesta aceptada

Lei Hou
Lei Hou el 18 de Nov. de 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]}

Más respuestas (1)

David Hill
David Hill el 3 de Nov. de 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 comentario
Youssif Youssif
Youssif Youssif el 3 de Nov. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by