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:
accumarray, histc, bin in non-monotonic bins?

Subject: accumarray, histc, bin in non-monotonic bins?

From: Rhys

Date: 27 May, 2010 15:03:07

Message: 1 of 5

Ive been playing with histc and accumarray, but think they will not do this.

Is there an easy way to bin data in non-monotonic, unevenly distributed bins

I want to count the number of ones in data(:,1) into bins defined by data(:,2). A snippet of real data is below.

So the question is, for the data below, count how many ones there are for 135045, for 135225, etc.

any ideas, or is a relable of the bins in order, so they are monotonic, then accumarray or histc?

data = [1 135045
           1 135225
           1 45135
           0 135225
           0 45135
           1 45315
           0 135225
           0 315045
           1 315045
           1 225315
           1 135045
           0 135225
           0 135045
           0 135045
           1 225135
           1 315225
           1 315225
           0 45315
           0 315225
           1 45135
           0 225135
           0 45135
           1 315045
           1 315045
           1 45135
           1 45315
           0 45315
           1 225135
           1 225315
           1 135045
           0 135225
           0 315225
           1 225135
           1 225315
           1 225135
           1 315045
           1 315225
           0 45315]

Subject: accumarray, histc, bin in non-monotonic bins?

From: Oleg Komarov

Date: 27 May, 2010 15:40:23

Message: 2 of 5

"Rhys " <rhyswork@yahoo.co.uk> wrote in message <htm1jb$5bf$1@fred.mathworks.com>...
> Ive been playing with histc and accumarray, but think they will not do this.
>
> Is there an easy way to bin data in non-monotonic, unevenly distributed bins
>
> I want to count the number of ones in data(:,1) into bins defined by data(:,2). A snippet of real data is below.
>
> So the question is, for the data below, count how many ones there are for 135045, for 135225, etc.
>
> any ideas, or is a relable of the bins in order, so they are monotonic, then accumarray or histc?

I'll give a solution using accumarray but histc can be used as well in a similar manner:

% Retrieve bins and subs for accumarray (common to histc method)
[unSubs,trash,subs] = unique(data(:,2));

% Accumulate with accumarray
Out = [unSubs accumarray(subs,data(:,1))]

Out =
       45135 3
       45315 2
      135045 3
      135225 1
      225135 4
      225315 3
      315045 4
      315225 3

Oleg

Subject: accumarray, histc, bin in non-monotonic bins?

From: Walter Roberson

Date: 27 May, 2010 15:44:20

Message: 3 of 5

Rhys wrote:
> Ive been playing with histc and accumarray, but think they will not do
> this.
>
> Is there an easy way to bin data in non-monotonic, unevenly distributed
> bins
>
> I want to count the number of ones in data(:,1) into bins defined by
> data(:,2).

accumarray should work for that.

T = accumarray(data(:,2), data(:,1));
locs = find(T);
poscounts = T(locs);
zerocount = setdiff(data(:,2), locs);

Subject: accumarray, histc, bin in non-monotonic bins?

From: Sean

Date: 27 May, 2010 15:48:25

Message: 4 of 5

"Rhys " <rhyswork@yahoo.co.uk> wrote in message <htm1jb$5bf$1@fred.mathworks.com>...
> Ive been playing with histc and accumarray, but think they will not do this.
>
> Is there an easy way to bin data in non-monotonic, unevenly distributed bins
>
> I want to count the number of ones in data(:,1) into bins defined by data(:,2). A snippet of real data is below.
>
> So the question is, for the data below, count how many ones there are for 135045, for 135225, etc.
>
> any ideas, or is a relable of the bins in order, so they are monotonic, then accumarray or histc?
>
> data = [1 135045
> 1 135225
> 1 45135
> 0 135225
> 0 45135
> 1 45315
> 0 135225
> 0 315045
> 1 315045
> 1 225315
> 1 135045
> 0 135225
> 0 135045
> 0 135045
> 1 225135
> 1 315225
> 1 315225
> 0 45315
> 0 315225
> 1 45135
> 0 225135
> 0 45135
> 1 315045
> 1 315045
> 1 45135
> 1 45315
> 0 45315
> 1 225135
> 1 225315
> 1 135045
> 0 135225
> 0 315225
> 1 225135
> 1 225315
> 1 225135
> 1 315045
> 1 315225
> 0 45315]


data = [1 135045
           1 135225
           1 45135
           0 135225
           0 45135
           1 45315
           0 135225
           0 315045
           1 315045
           1 225315
           1 135045
           0 135225
           0 135045
           0 135045
           1 225135
           1 315225
           1 315225
           0 45315
           0 315225
           1 45135
           0 225135
           0 45135
           1 315045
           1 315045
           1 45135
           1 45315
           0 45315
           1 225135
           1 225315
           1 135045
           0 135225
           0 315225
           1 225135
           1 225315
           1 225135
           1 315045
           1 315225
           0 45315];
       
 [udata, trash, ix] = unique(data(:,2));
 A = accumarray(ix,data(:,1));
 
 results = [udata, A];

Subject: accumarray, histc, bin in non-monotonic bins?

From: Rhys

Date: 28 May, 2010 10:06:04

Message: 5 of 5

Thanks very much both.

Learnt something from both sets of code.

The code I have gone with is

MISSdata = abs(data(:,1) - 1);
 [trial, trash, trial_indx] = unique(data(:,2));
 hit_count = accumarray(trial_indx,data(:,1));
 miss_count = accumarray(trial_indx,MISSdata);
 results = [trial, hit_count, miss_count];

Thanks again.

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