Bin X data, sum corresponding Y data and then plot.

조회 수: 7 (최근 30일)
aneps
aneps 2015년 8월 3일
편집: aneps 2015년 8월 4일
I have X data ranging from 0 to 999 and corresponding Y data. I want to bin the X data from 0 to 2000 with a bin size of 10. Then accumulate corresponding Y data in the respective bins. For example, X data from 0.1 to 10 should be taken as 10; 10.01 till 20 should be taken as 20 etc. The Y data in the same interval should be summed up and put in the corresponding bins. At the end, I want to plot X,Y . Please help me to code this... thank you.

채택된 답변

Walter Roberson
Walter Roberson 2015년 8월 4일
binnum = ceil(X/10);
binsum = accumarray(binnum(:), Y(:));
plot((1:size(binsum,1))*10, binsum);

추가 답변 (1개)

Jan
Jan 2015년 8월 3일
edges = [0.1, 10, 20]; % However you define this...
[N, edges, bin] = histcounts(X, edges);
Result = accumarray(bin, Y);
I cannot check this currently. Perhaps you have to transpose an input of accumarray.
  댓글 수: 1
aneps
aneps 2015년 8월 4일
편집: aneps 2015년 8월 4일
I tried with this code but it is giving error:
Error:
[N, edges, bin] = histcounts(X, edges);
My code:
A = load('/Users/Myfolder/Documents/MATLAB/Data.txt');
X = A(:,1);
Y = A(:,2);
edges = [0.1, 10, 20];
[N, edges, bin] = histcounts(X, edges);
Result = accumarray(bin, Y);
Also, could you please explain me how the 'edges' is defined. It should be between max value and min value of X?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by