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:
Grouping Data

Subject: Grouping Data

From: Roger Jones

Date: 10 Aug, 2010 13:05:20

Message: 1 of 4

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

Subject: Grouping Data

From: Sean

Date: 10 Aug, 2010 13:19:05

Message: 2 of 4

"Roger Jones" <leave@mealone.com> wrote in message <i3riqg$ba6$1@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.

Subject: Grouping Data

From: us

Date: 10 Aug, 2010 13:28:04

Message: 3 of 4

"Roger Jones" <leave@mealone.com> wrote in message <i3riqg$ba6$1@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

Subject: Grouping Data

From: Roger Jones

Date: 10 Aug, 2010 13:56:04

Message: 4 of 4

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.

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