필터 지우기
필터 지우기

vector discretization to sum of values in each bin

조회 수: 17 (최근 30일)
Amit Ifrach
Amit Ifrach 2021년 10월 4일
댓글: Matt J 2021년 10월 4일
לק"י
Hi guys, I'm looking a way to discretize vector values to bins. I want to have bins that contain the sum of values of incidents each of them contain.
example:
lets take the matlab's discretize example:
data = [1 1 2 3 6 5 8 10 4 4]
edges = 2:2:10
so, edges is:
edges = 1×5
2 4 6 8 10
Y = discretize(data,edges)
so, Y is:
Y = 1×10
NaN NaN 1 1 3 2 4 4 2 2
what I want to get is a vector that sums all the values of incidents that fall into each bin. for this example:
desiredvector=[5, 13, 6, 18]
as you can see, I have 4 bins (as specified by edges), and in each bin i got the sum of values of the incidents that fall into it.
I hope I explained it well.. thanks any way!
Amit.

채택된 답변

Matt J
Matt J 2021년 10월 4일
편집: Matt J 2021년 10월 4일
data = [1 1 2 3 6 5 8 10 4 4];
edges = 2:2:10;
Y = discretize(data,edges);
valid=isfinite(Y);
result=accumarray(findgroups(Y(valid)).' , data(valid).').'
result = 1×4
5 13 6 18
or
result=splitapply(@sum , data(valid) , findgroups(Y(valid)) )
result = 1×4
5 13 6 18
  댓글 수: 2
Amit Ifrach
Amit Ifrach 2021년 10월 4일
לק"י
thank you very much!!
Matt J
Matt J 2021년 10월 4일
You're welcome, but please Accept-click the answer to certify that it resolved your question.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by