現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Re-bin My Data is Discrete Energy Values
3 ビュー (過去 30 日間)
古いコメントを表示
Chad
2011 年 12 月 16 日
Hi I am trying to rebin my data is discrete values.
Energy Amount
11.8652 3.5891
12.5513 6.6741
11.8652 3.5891
8.8125 1.1711
8.8125 2.3422
8.8125 1.1711
5.7598 0.3324
5.0737 0.9444
5.7598 0.3324
12.5513 6.6741
14.1000 19.0596
12.5513 6.6741
8.8125 2.3422
0.0000 0.0000
8.8125 2.3422
5.0737 0.9444
3.5250 7.2712
5.0737 0.9444
11.8652 3.5891
12.5513 6.6741
11.8652 3.5891
8.8125 1.1711
8.8125 2.3422
8.8125 1.1711
5.7598 0.3324
5.0737 0.9444
5.7598 0.3324
I am very thankful that Walter Roberson helped me with < * http://www.mathworks.com/matlabcentral/answers/22879-binning-for-a-histogram a very useful technique> as follows.
[uvals, a, uidx] = unique(YourData(:,1));
Sum = accumarray(uidx, YourData(:,2));
plot(uvals, Sum)
However, I am unable to bin the data in discrete bins. For example, I would like to re-bin my data from 1-2 Energy and then 2-3 Energy so and so. Bascially, I would like a histogram that shows the Energy values in the x-axis. I hope this is clear.
採用された回答
Walter Roberson
2011 年 12 月 16 日
Sum = histc(YourData, BinBoundaries);
18 件のコメント
Walter Roberson
2011 年 12 月 16 日
Sorry, your question was not clear. Try
[unneeded, binidx] = histc(YourData(:,1), BinBoundaries);
Sum = accumarray(binidx(:), YourData(:,2));
plot(BinBoundaries, Sum);
However, if you have any values that are exactly equal to the largest bin boundary you specified, then Sum will have one value more than BinBoundaries, so you will need to account for that.
Chad
2011 年 12 月 19 日
Hi Walter,
Your first method work really well. For example,
Energy Amount
1.2 3.2
1.2 4.5
1.5 1.2
2.1 3.2
2.7 2.5
4.5 1.2
4.5 3.5
6.5 3.0
7.3 2.1
9.1 1.2
9.6 3.5
And the code you gave me originally works great such as,
Energy Amount
1.2 7.7
1.5 1.2
2.1 3.2
2.7 2.5
4.5 4.7
6.5 3.0
7.3 2.1
9.1 1.2
9.6 3.5
So the energy values of 1.2 and 4.5 are the same and they are added. This works great for my code. But what I am struggling with is now binning the data for example,
Energy Amount
1-2 8.9
2.1-3 5.7
3.1-4 0
4.1-5 4.7
5.1-6 0
6.1-7 3.0
7.1-8 2.1
8.1-9 0
9.1-10 4.7
Then plot(Energy,Amount). I hope this is much clearer. And thank you very much. You have been very helpful.
Walter Roberson
2011 年 12 月 19 日
What do those "Energy" values indicate? They appear to be bin boundaries and in the sample data you show, they are unique, and thus appear to already be binned. What would you like done?
Chad
2011 年 12 月 19 日
Yes..the sample data is unique. I am just trying to put the two values for Energy and Amount 1.2 and 1.5 into one bin with their values added together. So one bin of from Energy 1-2 will have the 7.7 + 1.2. It seems very simple but I cannot get it to work.
Walter Roberson
2011 年 12 月 19 日
Read the first column as text and throw away the '-' to the end of the column, and convert the resulting strings to a vector. histc() that vector against the same BinBoundaries as you used before,
[unneeded, binidx] = histc(TheEnergies, BinBoundaries);
Read the second column, Amount, as numbers (this can be done at the same time you read the first column).
Then,
NewSum = accumarray(binidx(:), TheAmounts(:), size(Sum));
Sum = Sum + NewSum;
I used accumarray just in case the bins were not exactly the same, such as if the new file did not extend as far as the existing data. Also, I noticed that the new file has 1-2 as the first bin whereas 1.1-2 is what would be expected based upon the other entries; this indicates the potential for a bin numbering difference from 1 to 1.1
Chad
2011 年 12 月 19 日
My real numbers are very random. So it really should be 1-1.99999 for one bin and then 2-2.9999 for then next. I guess I was not clear. Also, I was hoping to plot this as a histgram according to the energy bin. So maybe I should not be using plot. What do you suggest?
Walter Roberson
2011 年 12 月 19 日
I said to read them as text because MATLAB is not able to store a number that has a - sign in the middle such as '2.1-3' . Whether you intended it to or not, there is no space in that between the '1' and the '-' so it has to have been stored as text. If you already have it as a text string then just proceed to throwing away the '-' onwards.
Histogram:
hist(BinBoundaries, Sum);
Walter Roberson
2011 年 12 月 19 日
I said to read them as text because MATLAB is not able to store a number that has a - sign in the middle such as '2.1-3' . Whether you intended it to or not, there is no space in that between the '1' and the '-' so it has to have been stored as text. If you already have it as a text string then just proceed to throwing away the '-' onwards.
Histogram:
bar(BinBoundaries, Sum);
Chad
2011 年 12 月 19 日
Alright...I am still a little confused. I think I confused you more. Anyway, I am very close to getting this. I first start with,
Energy Amount
1.2 3.2
1.2 4.5
1.5 1.2
2.1 3.2
2.7 2.5
4.5 1.2
4.5 3.5
6.5 3.0
7.3 2.1
9.1 1.2
9.6 3.5
I then use the first set of code you provided to do the following.
Energy Amount
1.2 7.7
1.5 1.2
2.1 3.2
2.7 2.5
4.5 4.7
6.5 3.0
7.3 2.1
9.1 1.2
9.6 3.5
This is now the discrete energy value and the amounts added up. This is what I was looking for. Now I want to re-bin and I do the following.
[unneeded, binidx] = histc(Energy, 1:1:14) which gives,
[unneeded, binidx] = histc(Energy, 1:1:14)
unneeded =
2 2 0 1 0 1 1 0 2 0 0 0 0 0
binidx =
1 1 2 2 4 6 7 9 9
And this is correct. I then do the following.
Sum = accumarray(binidx(:), Amount)
Which gives,
Sum =
8.9000
5.7000
0
4.7000
0
3.0000
2.1000
0
4.7000
Which is what I was looking for. I then do bar(Sum) and this is correct. But, what does not work and I have no idea why is when I change from (1:.1:14). I do the same steps and my bar plot is out to 89..not 8.9. I am confused by that.
Chad
2011 年 12 月 19 日
Wait..I had that wrong. My bins are correct..but the x-scale is off. For exampe, where I expect Energy (x-value) at 1.2 = 7.7 I get x = 3 and y = 7.7. I hope I am not confusing you...:)
Chad
2011 年 12 月 19 日
Basically...when I do bar(BinBoundaries, Sum) it complains that the lengths are not the same. And they are not. So this works except the x-scale is off.
Walter Roberson
2011 年 12 月 19 日
You need to pass your bin boundaries as the first parameter to bar()
For example,
bar(1:.1:14, Sum)
Chad
2011 年 12 月 19 日
I do...but when I do
Sum =
8.9000
5.7000
0
4.7000
0
3.0000
2.1000
0
4.7000
I only get 9 values. 10,11,12,13,14 have been excluded. Therefore, BinBoundaries is (1:1:14) is 14 values...so what can i do keep the zero's? And I really appreaciate your help..
Walter Roberson
2011 年 12 月 19 日
Replace
Sum = accumarray(binidx(:), Amount)
with
Sum = accumarray(binidx(:), Amount, length(unneeded))
Chad
2011 年 12 月 19 日
Sum = accumarray(binidx(:), Amount, length(unneeded))
??? Error using ==> accumarray
Third input SZ must be a full row vector with one element for each column of SUBS.
I keep getting this error. I cannot find accumarry in my help. But it works. That is odd.
Chad
2011 年 12 月 19 日
Okay..I found it in my help. but I cannot get the zero's I need...so frustating..
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Distribution 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)