# short way to bar plot a discretize vector?

6 Ansichten (letzte 30 Tage)
Amit Ifrach am 7 Feb. 2023
Kommentiert: Jan am 9 Feb. 2023
לק"י
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.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Jan am 7 Feb. 2023
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 Kommentare3 ältere Kommentare anzeigen3 ältere Kommentare ausblenden
Amit Ifrach am 9 Feb. 2023
Hi @Jan,
It's Yemenite jews way to start a writing (translates somewhat to for the hope for g-d)
Amit :)
Jan am 9 Feb. 2023
@Amit Ifrach: Thanks for the explanation. I assume I guess correctly, what "g-d" is.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Annotations finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by