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!
採用された回答
Matt J
2018 年 5 月 16 日
Use splitapply() for the groupwise mean and bar() to generate the bar plot.
14 件のコメント
leonidas86
2018 年 5 月 16 日
Im running Matlab 2012b and can't use the function splitapply...
Matt J
2018 年 5 月 16 日
In that case, you can emulate it using accumarray().
[~,~,subs]=unique(groupIDs);
groupMeans = accumarray(subs,rightcolumn,[],@mean);
leonidas86
2018 年 5 月 16 日
At the unique line I get the error that the subscript indices must either be real positive integers or logicals.
Jan
2018 年 5 月 16 日
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.
leonidas86
2018 年 5 月 16 日
No I dont use unique as a variable name.
Matt J
2018 年 5 月 16 日
We would need to see code and error messages.
leonidas86
2018 年 5 月 17 日
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.
Matt J
2018 年 5 月 17 日
Since pPos are not integers, they cannot be used as indices to the array 'complete'.
leonidas86
2018 年 5 月 17 日
How i can I convert pPos to integers? Can I use "floor"?
Jan
2018 年 5 月 17 日
It depends on what you need. If floor is matching, use floor.
leonidas86
2018 年 5 月 17 日
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?
Matt J
2018 年 5 月 17 日
You could also use histc, e.g.,
[~,groupID]=histc(pPos,0:10:100);
leonidas86
2018 年 5 月 17 日
編集済み: leonidas86
2018 年 5 月 17 日
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?
leonidas86
2018 年 5 月 17 日
I made it:
xTick=round(groupPosMeans);
set(gca, 'XTick', xTick);
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Box Plots についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
