From: Rhys on
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]
From: Oleg Komarov on
"Rhys " <rhyswork(a)yahoo.co.uk> wrote in message <htm1jb$5bf$1(a)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
From: Walter Roberson on
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);

From: Sean on
"Rhys " <rhyswork(a)yahoo.co.uk> wrote in message <htm1jb$5bf$1(a)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];
From: Rhys on
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.