How to group data in two column matrix

Hello, I have a set of data that I would like to analyse. The data is split into two columns in a matrix. Here is a cutout:
16,7115184526445 1,99856679248824
16,7765766713840 1,90380826928436
16,8203225632002 1,90759997561475
16,9296872927408 1,93705728785794
17,0247496340434 1,95221030451463
17,0840560047515 1,97156344341112
17,1728721040053 2,02042344983047
17,2760126971073 2,04365662563518
17,3427845325050 1,99056685824960
17,3555146973138 1,96334283757260
17,3614789932988 1,92935199439745
17,3759202152813 1,94379321637992
17,4269340102000 1,91489959400124
17,4440061891234 9,29514481185319
17,5122505102985 1,76049098118472
17,5544684974967 1,96252475665197
17,5760555781573 1,94415765182676
17,6300232798089 1,95817212166681
17,6986099837662 1,66717115625156
17,7054927064216 1,99368640911937
17,7158167904047 1,92410307580507
17,7416270003624 1,94991328576274
17,8656023527728 1,99398026720148
Now I want to group the left data into 10 classes like 10,20,30,40 etc... and calculate the mean values of the right data for each group. In a next step I want to plot the data with a bar for each group. I have sort the matrix according to the left column with sortrows. How can i go on?
Thanks for your help!

 Accepted Answer

Matt J
Matt J on 16 May 2018
Use splitapply() for the groupwise mean and bar() to generate the bar plot.

14 Comments

Im running Matlab 2012b and can't use the function splitapply...
In that case, you can emulate it using accumarray().
[~,~,subs]=unique(groupIDs);
groupMeans = accumarray(subs,rightcolumn,[],@mean);
At the unique line I get the error that the subscript indices must either be real positive integers or logicals.
Then you have redefined "unique" as a variable. Check this by:
which unique -all
The solution is the use a different name for your variable.
No I dont use unique as a variable name.
We would need to see code and error messages.
Subscript indices must either be real positive integers or logicals.
Error in histogram_test (line 71) [~,~,subs]=unique(complete(pPos));
pPos includes the left column of the matrix above.
Since pPos are not integers, they cannot be used as indices to the array 'complete'.
How i can I convert pPos to integers? Can I use "floor"?
It depends on what you need. If floor is matching, use floor.
Thanks for your help. Now it works.
pPosr=round(pPos);
[~,~,subs]=unique(pPosr);
groupMeans = accumarray(subs,pDiff,[],@mean);
The unique function actually creates 59 groups. Is it possible to create n groups?
You could also use histc, e.g.,
[~,groupID]=histc(pPos,0:10:100);
I have one last question about plotting the histogram.
groupPosMeans = accumarray(groupID,pPos,[],@mean);
Above I calculated my x axis values. Then plotting with:
bar(groupPosMeans,groupDiffMeans);
How can I set the right x axis scale according to groupPosMeans?
I made it:
xTick=round(groupPosMeans);
set(gca, 'XTick', xTick);

Sign in to comment.

More Answers (0)

Asked:

on 16 May 2018

Commented:

on 17 May 2018

Community Treasure Hunt

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

Start Hunting!