groupby of one column

31 ビュー (過去 30 日間)
Venkata Podduturi
Venkata Podduturi 2012 年 5 月 8 日
編集済み: neuromechanist 2021 年 2 月 25 日
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

回答 (2 件)

neuromechanist
neuromechanist 2020 年 7 月 14 日
編集済み: neuromechanist 2021 年 2 月 25 日
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 件のコメント
frankovaT
frankovaT 2021 年 2 月 3 日
I needed it thanks
neuromechanist
neuromechanist 2021 年 2 月 3 日
You are welcome. I appreciate it if you can upvote it, so others will find the answer easier.

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


Geoff
Geoff 2012 年 5 月 8 日
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.

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by