Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
add histograms

Subject: add histograms

From: Greg Thom

Date: 29 Dec, 2009 16:38:02

Message: 1 of 4

hello there

can I use accumarray to add histograms with different number of bins but same bin widths ?

Say I have the following two *histograms*

dbins1 = 0.0250 0.0750 0.1250 0.1750 0.2250 0.2750 0.3250 0.3750 0.4250 0.4750;
histv1 = 4 2 10 3 20 50 31 11 22 66;



dbins2 = 0.2250 0.2750 0.3250 0.3750 0.4250 0.4750 0.5250 0.5750 0.6250 0.6750 0.7250 0.7750 0.8250;
histv2 = 6 55 10 45 10
 2 14 7 6 20 45 8 4;


and I need to add them out making sure I add only histv values of the same dbins. Can this be achieved without loops ? using accumarray or other vectorized matlab code?

Thank you

Subject: add histograms

From: ImageAnalyst

Date: 29 Dec, 2009 17:00:35

Message: 2 of 4

No you can't. If the bins don't line up, you don't know where a value
in a bin in one histogram would have come from if it had been sampled
with the bins from the other histogram. The only way would be the
special case where one hist had bins that were perfect multiples of
the other, for example hist 2 had three bins for every bin of hist 1
and the edges of the bins lined up perfectly. In the general (non-
aligning) case, you could use interp1() to try to get an estimate of
one histogram along the x axis of the other but with the low values of
numbers you're showing, that wouldn't be very accurate at all.

Your best bet is to use histc() to get histograms that have the same
bin locations.

Subject: add histograms

From: Greg Thom

Date: 29 Dec, 2009 19:37:04

Message: 3 of 4

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <50af785d-3d3f-4336-8d74-d9efce05be43@26g2000yqo.googlegroups.com>...
> No you can't. If the bins don't line up, you don't know where a value
> in a bin in one histogram would have come from if it had been sampled
> with the bins from the other histogram. The only way would be the
> special case where one hist had bins that were perfect multiples of
> the other, for example hist 2 had three bins for every bin of hist 1
> and the edges of the bins lined up perfectly. In the general (non-
> aligning) case, you could use interp1() to try to get an estimate of
> one histogram along the x axis of the other but with the low values of
> numbers you're showing, that wouldn't be very accurate at all.
>
> Your best bet is to use histc() to get histograms that have the same
> bin locations.

Hello , thanks for your answer, I see your point. Now what I have done is that I have scanned through all histograms and made one bins location array that spans all the observed bins locations.
In a naive way, I will simply loop over all histogram pairs, find the location of the bins location within the base bins locatioins array, then add their histv values ,

say I have determined that all bins locatioins fall within

dbins0 = 0.2250:0.05:0.8250
histv0 = zeros(size(dbins0));

Then I now have :

dbins1 = 0.3250 0.3750 0.4250 0.4750 0.5250 0.5750 0.6250 0.6750 0.7250 0.7750 ;
histv1 = 55 10 2 5 10 14 7 6 20 45 4 ;

and

dbins2 = 0.3250 0.3750 0.4250 0.4750 0.5250 0.5750 ;
histv12 = 4 63 5 2 10 55 ;

Can these now be added without using a loop somehow ?

Thanks

Subject: add histograms

From: ImageAnalyst

Date: 29 Dec, 2009 19:58:24

Message: 4 of 4

Greg:
Well as long as they both start at the same x value, like you showed
here, then you can do this:
workspace; % Display workspace panel.
% Create the data
histv1 = [55 10 2 5 10 14 7 6 20 45 4]
histv12 = [ 4 63 5 2 10 55]
% Get the lengths.
length1 = length(histv1)
length2 = length(histv12)
% Do the sum.
if length1 >= length2
sumOfHists = histv1;
sumOfHists(1:length2) = sumOfHists(1:length2) + histv12(1:length2);
else
sumOfHists = histv12;
sumOfHists(1:length1) = sumOfHists(1:length1) + histv1(1:length1);
end
sumOfHists % Display results in command window.

If they start and end at different x values, then you'll have to
decide the starting and ending bin over which to do the addition.
Regards,
ImageAnalyst

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us