לק"י
Hello!
I want to take a vector A, and bar plot it using specific bins (defiend by 'edges' vector). I want the plot to show the sum of values that belong to each bin. Not the number of values like histogram does.
I started to write a code:
dsc=discretize(A, edges);
barpltcll=num2cell(edges);
for i=length(edges) %this part creates cell array that holds all the sum of values that fall in the bins.
barpltcll(2,i)=sum(A(dsc(i)))...
but I thought maybe there is shorter way to do it. I would love to know about it, like turn an option in the histogram() comamnd to show the sum of values that fell into the bin rather then the counts (number of values). :)
Thanks,
Amit.

 채택된 답변

Jan
Jan 2023년 2월 7일
A = rand(1, 100);
edges = linspace(0, 1, 6);
dsc = discretize(A, edges);
result = accumarray(dsc(:), A(:)); % Default function: @sum
bar(edges(1:end-1), result)

댓글 수: 5

Amit Ifrach
Amit Ifrach 2023년 2월 7일
편집: Amit Ifrach 2023년 2월 7일
לק"י
Hi Jan, thanks for the quick answer!
May I just ask another thing.. when using accumarray i got this:
Error using accumarray
First input SUBS must contain positive integer subscripts.
Any idea why do I got it?
I read a bit about it, something about 'unique' values which I belive I don't have and as far as I saw, there are only positive integers in the dsc variable, like here:
Thanks :)
Amit Ifrach
Amit Ifrach 2023년 2월 7일
לק"י
Sorry, had to find the nan (max value).
Thank :)
Jan
Jan 2023년 2월 7일
@Amit Ifrach: What does this mean: לק"י
Amit Ifrach
Amit Ifrach 2023년 2월 9일
Hi @Jan,
It's Yemenite jews way to start a writing (translates somewhat to for the hope for g-d)
Amit :)
Jan
Jan 2023년 2월 9일
@Amit Ifrach: Thanks for the explanation. I assume I guess correctly, what "g-d" is.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

질문:

2023년 2월 7일

댓글:

Jan
2023년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by