COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Splitting data in Matlab

• Email
• Follow

```Hi everyone,
I have a dataset that looks like this:
Group      Value
3             6
2             5
3             8
1             2
1             1
2             5
3             7

I want a script that willl pull all the "group 1's" and calculate the mean and stdev of that population, then all the group 2's and group 3's.

I the end, I want to have something that looks like this:
Group          Mean        Stdev
1                 X              Y
2                 A              B
3                 C              D

What would be the best way to do this?  Especially a way that doesn't involve for loops?

Thanks!
```
 0

See related articles to this posting

```"Anna Chen" <icedredtea@yahoo.com> wrote in message <hqn6cc\$eme\$1@fred.mathworks.com>...
> Hi everyone,
> I have a dataset that looks like this:
> Group      Value
> 3             6
> 2             5
> 3             8
> 1             2
> 1             1
> 2             5
> 3             7
>
> I want a script that willl pull all the "group 1's" and calculate the mean and stdev of that population, then all the group 2's and group 3's.
>
> I the end, I want to have something that looks like this:
> Group          Mean        Stdev
> 1                 X              Y
> 2                 A              B
> 3                 C              D
>
> What would be the best way to do this?  Especially a way that doesn't involve for loops?
>
> Thanks!

' accumarray' might help
```
 0

```"Anna Chen" <icedredtea@yahoo.com> wrote in message <hqn6cc\$eme\$1@fred.mathworks.com>...
> Hi everyone,
> I have a dataset that looks like this:
> Group      Value
> 3             6
> 2             5
> 3             8
> 1             2
> 1             1
> 2             5
> 3             7
>

to find the mean of all the group one values, you can do this:

mean(dataset(dataset(:,1)==1,2))
```
 0

```A=[3 6;
2 5;
3 8;
1 2;
1 1;
2 5;
3 7];

% Engine
[U G G]=unique(A(:,1));
m0=accumarray(G,1);
m1=accumarray(G,A(:,2));
m2=accumarray(G,A(:,2).^2);
mU=m1./m0;
sU=(m2-m1.*mU)./(m0-1);

[U mU sU]

% Bruno
```
 0

```"Anna Chen" <icedredtea@yahoo.com> wrote in message <hqn6cc\$eme\$1@fred.mathworks.com>...
> Hi everyone,
> I have a dataset that looks like this:
> Group      Value
> 3             6
> 2             5
> 3             8
> 1             2
> 1             1
> 2             5
> 3             7
>
> I want a script that willl pull all the "group 1's" and calculate the mean and stdev of that population, then all the group 2's and group 3's.
>
> I the end, I want to have something that looks like this:
> Group          Mean        Stdev
> 1                 X              Y
> 2                 A              B
> 3                 C              D
>
> What would be the best way to do this?  Especially a way that doesn't involve for loops?
>
> Thanks!

one of the other solutions

% the data
gv=[
3     6
2     5
3     8
1     2
1     1
2     5
3     7
];
% the engine
gu=unique(gv(:,1));
gm=arrayfun(@(x) mean(gv(gv(:,1)==x,2)),gu);
r=[gu,gm];
% the result
disp(r);
%{
1          1.5
2            5
3            7
%}

us
```
 0