groupby of one column

41 visualizaciones (últimos 30 días)
Venkata Podduturi
Venkata Podduturi el 8 de Mayo de 2012
Editada: neuromechanist el 25 de Feb. de 2021
I have a file called msg.txt which has three columns:
message_no instance_no delay
0 1 1228
1 1 1240
2 1 3304
3 1 5320
4 1 7324
0 2 1232
2 2 3308
0 3 1236
2 3 3300
4 2 328
1 2 1080
0 4 1228
2 4 3304
3 2 5320
0 5 1232
2 5 3308
0 6 1236
2 6 3300
1 3 1076
4 3 3328
0 7 1228
2 7 3304
3 3 5320
0 8 1232
2 8 3308
4 4 328
0 9 1236
1 4 1072
2 9 3300
0 10 1228
2 10 3304
3 4 5320
0 11 1232
2 11 3308
4 5 1324
0 12 1236
1 5 1248
2 12 3300
0 13 1228
2 13 3304
Now i want group all message by column1 i.e by there message_no and plot the graph between instance_no and delay of the corresponding message_no. Suppose if we consider message_no=2, then it has 13 instances and delays, and i have plot the graph between instance_no and delays of message_no=2.
Thank You, Venkata

Respuestas (2)

neuromechanist
neuromechanist el 14 de Jul. de 2020
Editada: neuromechanist el 25 de Feb. de 2021
This is about eight years late. But for anybody who may stumble upon this question, look in to findgroups function. It creates groups based on the content of table column (or any variable). Then you can use the results with splitapply to apply any aggreagate fucntion such as mean, max, sum, etc.
  2 comentarios
frankovaT
frankovaT el 3 de Feb. de 2021
I needed it thanks
neuromechanist
neuromechanist el 3 de Feb. de 2021
You are welcome. I appreciate it if you can upvote it, so others will find the answer easier.

Iniciar sesión para comentar.


Geoff
Geoff el 8 de Mayo de 2012
Selection is different from grouping. If you just want instance_no and delays where message_no=2, you can do this (assuming you have already read the file into a matrix called data):
xy = data(data(:,1)==2, [2 3]);
But to get all the groups....
I don't know any fancy grouping functions off-hand, but if you don't have loads of data, why not do the same thing. It's not particularly efficient but it's easy:
messages = unique(data(:,1));
xys = arrayfun( @(m) data(data(:,1)==m, [2 3]), messages, 'UniformOutput', 0 );
If you want to be a bit more efficient (maybe you have lots of data), you could sort by message number first and then partition the data set:
sdata = sortrows(data);
endidx = find(diff(sdata(:,1)) ~= 0);
r = [1 endidx'; endidx' size(sdata,1)];
idx = arrayfun( @(b) r(1,b):r(2,b), 1:size(r,2), 'UniformOutput', 0 );
xys = cellfun( @(ii) data(ii,[2 3]), idx, 'UniformOutput', 0 );
The above finds the indices of the last number in each set (by detecting when the value changes). It then builds the 'range' matrix r whos first row is the start-index and second row is the end-index of each set. Then an cell-array of index ranges for each set, idx, is created and that is used to pull the required two columns out of the data.

Categorías

Más información sobre Structures 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