From: Roger Jones on
Hi all,

I know that [n xout] = hist(vars, thresholds) will give me a frequency for each group. However, what I want to do is slightly different.

I have a 2 x many matrix of assosiated values. I want to group the data in column two according to which range of values it's column one value is.

eg.
vars= [1 3
2 435
3 40
2 20]

output in some format
from 0.5 to 1.5: 3
from 1.5 to 2.5: 435, 20
from 2.5 to 3.5: 40

Thanks
From: Sean on
"Roger Jones" <leave(a)mealone.com> wrote in message <i3riqg$ba6$1(a)fred.mathworks.com>...
> Hi all,
>
> I know that [n xout] = hist(vars, thresholds) will give me a frequency for each group. However, what I want to do is slightly different.
>
> I have a 2 x many matrix of assosiated values. I want to group the data in column two according to which range of values it's column one value is.
>
> eg.
> vars= [1 3
> 2 435
> 3 40
> 2 20]
>
> output in some format
> from 0.5 to 1.5: 3
> from 1.5 to 2.5: 435, 20
> from 2.5 to 3.5: 40
>
> Thanks

One way:

%Data
Vals = ceil(rand(10,2)*10)

%Engine
[~, ~, j] = unique(Vals(:,1));
accumarray(j,Vals(:,2),[],@(x){x})
%
%Each result in the end answer will correspond to that position value in j.
From: us on
"Roger Jones" <leave(a)mealone.com> wrote in message <i3riqg$ba6$1(a)fred.mathworks.com>...
> Hi all,
>
> I know that [n xout] = hist(vars, thresholds) will give me a frequency for each group. However, what I want to do is slightly different.
>
> I have a 2 x many matrix of assosiated values. I want to group the data in column two according to which range of values it's column one value is.
>
> eg.
> vars= [1 3
> 2 435
> 3 40
> 2 20]
> output in some format
> from 0.5 to 1.5: 3
> from 1.5 to 2.5: 435, 20
> from 2.5 to 3.5: 40
>
> Thanks

one of the many solutions

v=[
1 3
2 400
3 30
2 -400
];
[ix,ix]=histc(v(:,1),.5:1:3.5);
r=arrayfun(@(x) v(ix==x,2),unique(ix),'uni',false);
r{:}
%{
ans =
3
ans =
400
-400
ans =
30
%}

us
From: Roger Jones on
I don't really get Sean's, but thank you Us, your solution is very elegant.

Now I will have to read up on cells!
Regards.