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:
simple question and difficult in the same time

Subject: simple question and difficult in the same time

From: eesa

Date: 2 Nov, 2010 03:25:05

Message: 1 of 13

I have the data in array has three colums like the following:
user no profile no value
1 1 150
1 1 125
1 1 130
1 2 160
1 2 130
6 1 250
6 1 98
6 2 210
7 ... ...
..
..
 So, what i need is to get the outcome table has the median value grouping by the user no and profile no like the following
user no profile no median value
1 1 137
1 2 140
6 1 190
..

 i would appreciate if someone knows how i can solve this problem because i wasted 5 hours to sove it but i could not.

Rregards,
Eesa

Subject: simple question and difficult in the same time

From: ImageAnalyst

Date: 2 Nov, 2010 03:47:33

Message: 2 of 13

Do you have the Signal Processing Toolbox, with the 1D median filter
in it?

And I don't quite see how the median of 125, 130, and 150 is 137.5
(instead of 130), and how the median of 160 and 130 is 140, etc.

Subject: simple question and difficult in the same time

From: Ross W

Date: 2 Nov, 2010 03:56:04

Message: 3 of 13

"eesa " <alalyani@hotmail.com> wrote in message <iao0ah$ggl$1@fred.mathworks.com>...
> I have the data in array has three colums like the following:
> user no profile no value
> 1 1 150
> 1 1 125
> 1 1 130
> 1 2 160
> 1 2 130
> 6 1 250
> 6 1 98
> 6 2 210
> 7 ... ...
> ..
> ..
> So, what i need is to get the outcome table has the median value grouping by the user no and profile no like the following
> user no profile no median value
> 1 1 137
> 1 2 140
> 6 1 190
> ..
>
> i would appreciate if someone knows how i can solve this problem because i wasted 5 hours to sove it but i could not.
>
> Rregards,
> Eesa

Take a look at accumarray, especially example 6. http://www.mathworks.com/help/techdoc/ref/accumarray.html

Sorry I don't have time for sorting out the details in your case, but I think this is a useful tool for the job.

Ross

Subject: simple question and difficult in the same time

From: eesa

Date: 2 Nov, 2010 03:56:04

Message: 4 of 13

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <89666625-72d8-4e9e-ab5d-a22d7de7ae0f@u17g2000yqi.googlegroups.com>...
> Do you have the Signal Processing Toolbox, with the 1D median filter
> in it?
>
> And I don't quite see how the median of 125, 130, and 150 is 137.5
> (instead of 130), and how the median of 160 and 130 is 140, etc.

No, i do not have signal processing toolbox.
 i just put the median value in the example to show the output how it looks like but i agree with you it is not correct value.

Subject: simple question and difficult in the same time

From: ImageAnalyst

Date: 2 Nov, 2010 04:07:57

Message: 5 of 13

On Nov 1, 11:56 pm, "eesa " <alaly...@hotmail.com> wrote:
> No, i do not have signal processing toolbox.
>  i just put the median value in the example to show the output how it looks like but i agree with you it is not correct value.
---------------------------------------------------
And which value do you want to use as the median if there are only two
values for a particular userNumber/profileNumber combination - the
high one or the low one?

The unique() function may come in useful for you.

Maybe you can supply some code, at least some code to generate some
sample data. Come on, at least make it somewhat easy for us to help
you.

Subject: simple question and difficult in the same time

From: eesa

Date: 2 Nov, 2010 04:28:03

Message: 6 of 13

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <5d139762-ddab-420f-afad-feb2447597ae@f33g2000yqh.googlegroups.com>...
> On Nov 1, 11:56 pm, "eesa " <alaly...@hotmail.com> wrote:
> > No, i do not have signal processing toolbox.
> >  i just put the median value in the example to show the output how it looks like but i agree with you it is not correct value.
> ---------------------------------------------------
> And which value do you want to use as the median if there are only two
> values for a particular userNumber/profileNumber combination - the
> high one or the low one?
>
> The unique() function may come in useful for you.
>
> Maybe you can supply some code, at least some code to generate some
> sample data. Come on, at least make it somewhat easy for us to help
> you.
Median can be done for only two values and it returns the average for the two values.
in my case, i need the structure or the code in order to calculate the median value grouping usernum and profileno that calculate the median value for each user and all the profiles for that user.

Subject: simple question and difficult in the same time

From: eesa

Date: 2 Nov, 2010 05:10:05

Message: 7 of 13

"eesa " <alalyani@hotmail.com> wrote in message <iao40j$bq6$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <5d139762-ddab-420f-afad-feb2447597ae@f33g2000yqh.googlegroups.com>...
> > On Nov 1, 11:56 pm, "eesa " <alaly...@hotmail.com> wrote:
> > > No, i do not have signal processing toolbox.
> > >  i just put the median value in the example to show the output how it looks like but i agree with you it is not correct value.
> > ---------------------------------------------------
> > And which value do you want to use as the median if there are only two
> > values for a particular userNumber/profileNumber combination - the
> > high one or the low one?
> >
> > The unique() function may come in useful for you.
> >
> > Maybe you can supply some code, at least some code to generate some
> > sample data. Come on, at least make it somewhat easy for us to help
> > you.
> Median can be done for only two values and it returns the average for the two values.
> in my case, i need the structure or the code in order to calculate the median value grouping usernum and profileno that calculate the median value for each user and all the profiles for that user.

please guys if someone knows how i can solve this problem, i will pray for him. i tried several times but i could not and i need it uregently.

Subject: simple question and difficult in the same time

From: james bejon

Date: 2 Nov, 2010 05:11:03

Message: 8 of 13

Something like..

A = [1 1 150; 1 1 125; 1 1 130; 1 2 160; 1 2 130; 6 1 250; 6 1 98; 6 2 210]
[Key, Junk, Subs] = arrayfun(@(x) unique(A(:, x)), 1:2, 'UniformOutput', 0);
Res = accumarray(Subs, A(:, 3), cellfun(@length, Key), @median);
[Key{:}] = ndgrid(Key{end:-1:1});
Key = reshape(cat(2, [Key{end:-1:1}]), [], length(Key));
Tbl = [Key, reshape(transpose(Res), [], 1)]

I guess you could use unique(:, 'rows') to generate the key at the end, which would be shorter in terms of code, but it seems a bit inefficient.

Subject: simple question and difficult in the same time

From: Ross W

Date: 2 Nov, 2010 06:46:04

Message: 9 of 13

"james bejon" <jamesbejon@yahoo.co.uk> wrote in message <iao6h6$pkr$1@fred.mathworks.com>...
> Something like..
>
> A = [1 1 150; 1 1 125; 1 1 130; 1 2 160; 1 2 130; 6 1 250; 6 1 98; 6 2 210]
> [Key, Junk, Subs] = arrayfun(@(x) unique(A(:, x)), 1:2, 'UniformOutput', 0);
> Res = accumarray(Subs, A(:, 3), cellfun(@length, Key), @median);
> [Key{:}] = ndgrid(Key{end:-1:1});
> Key = reshape(cat(2, [Key{end:-1:1}]), [], length(Key));
> Tbl = [Key, reshape(transpose(Res), [], 1)]
>
> I guess you could use unique(:, 'rows') to generate the key at the end, which would be shorter in terms of code, but it seems a bit inefficient.

Another alternative?

A = [1 1 150; 1 1 125; 1 1 130; 1 2 160; 1 2 130; 6 1 250; 6 1 98; 6 2 210];

median_vals=accumarray(A(:,1:2),A(:,3),[],@median,NaN);
k=find(~isnan(median_vals));
[r,c]=ind2sub(max(A(:,1:2)),k);

[r c median_vals(k)]

ans =

     1 1 130
     6 1 174
     1 2 145
     6 2 210

Ross

Subject: simple question and difficult in the same time

From: eesa

Date: 2 Nov, 2010 08:05:06

Message: 10 of 13

"Ross W" <rosswoodskiwi@hotmail.com> wrote in message <iaoc3c$hhh$1@fred.mathworks.com>...
> "james bejon" <jamesbejon@yahoo.co.uk> wrote in message <iao6h6$pkr$1@fred.mathworks.com>...
> > Something like..
> >
> > A = [1 1 150; 1 1 125; 1 1 130; 1 2 160; 1 2 130; 6 1 250; 6 1 98; 6 2 210]
> > [Key, Junk, Subs] = arrayfun(@(x) unique(A(:, x)), 1:2, 'UniformOutput', 0);
> > Res = accumarray(Subs, A(:, 3), cellfun(@length, Key), @median);
> > [Key{:}] = ndgrid(Key{end:-1:1});
> > Key = reshape(cat(2, [Key{end:-1:1}]), [], length(Key));
> > Tbl = [Key, reshape(transpose(Res), [], 1)]
> >
> > I guess you could use unique(:, 'rows') to generate the key at the end, which would be shorter in terms of code, but it seems a bit inefficient.
>
> Another alternative?
>
> A = [1 1 150; 1 1 125; 1 1 130; 1 2 160; 1 2 130; 6 1 250; 6 1 98; 6 2 210];
>
> median_vals=accumarray(A(:,1:2),A(:,3),[],@median,NaN);
> k=find(~isnan(median_vals));
> [r,c]=ind2sub(max(A(:,1:2)),k);
>
> [r c median_vals(k)]
>
> ans =
>
> 1 1 130
> 6 1 174
> 1 2 145
> 6 2 210
>
> Ross

Hi Ross,
Thank you very much you did it. I can not beleive it you are so genious. May the god safe you in your life and make it easy for you.

By the way, is it possible to make it in order like the output shows like:
1 1 130
1 2 145
6 1 174
6 2 210

I would appreciate if you could help me on that and it can save my time.

Subject: simple question and difficult in the same time

From: eesa

Date: 2 Nov, 2010 08:15:05

Message: 11 of 13

"eesa " <alalyani@hotmail.com> wrote in message <iaogni$7ja$1@fred.mathworks.com>...
> "Ross W" <rosswoodskiwi@hotmail.com> wrote in message <iaoc3c$hhh$1@fred.mathworks.com>...
> > "james bejon" <jamesbejon@yahoo.co.uk> wrote in message <iao6h6$pkr$1@fred.mathworks.com>...
> > > Something like..
> > >
> > > A = [1 1 150; 1 1 125; 1 1 130; 1 2 160; 1 2 130; 6 1 250; 6 1 98; 6 2 210]
> > > [Key, Junk, Subs] = arrayfun(@(x) unique(A(:, x)), 1:2, 'UniformOutput', 0);
> > > Res = accumarray(Subs, A(:, 3), cellfun(@length, Key), @median);
> > > [Key{:}] = ndgrid(Key{end:-1:1});
> > > Key = reshape(cat(2, [Key{end:-1:1}]), [], length(Key));
> > > Tbl = [Key, reshape(transpose(Res), [], 1)]
> > >
> > > I guess you could use unique(:, 'rows') to generate the key at the end, which would be shorter in terms of code, but it seems a bit inefficient.
> >
> > Another alternative?
> >
> > A = [1 1 150; 1 1 125; 1 1 130; 1 2 160; 1 2 130; 6 1 250; 6 1 98; 6 2 210];
> >
> > median_vals=accumarray(A(:,1:2),A(:,3),[],@median,NaN);
> > k=find(~isnan(median_vals));
> > [r,c]=ind2sub(max(A(:,1:2)),k);
> >
> > [r c median_vals(k)]
> >
> > ans =
> >
> > 1 1 130
> > 6 1 174
> > 1 2 145
> > 6 2 210
> >
> > Ross
>
> Hi Ross,
> Thank you very much you did it. I can not beleive it you are so genious. May the god safe you in your life and make it easy for you.
>
> By the way, is it possible to make it in order like the output shows like:
> 1 1 130
> 1 2 145
> 6 1 174
> 6 2 210
>
> I would appreciate if you could help me on that and it can save my time.

Hi Ross,
I solve it and it is fixed. Thank you in advance.

Subject: simple question and difficult in the same time

From: Bruno Luong

Date: 2 Nov, 2010 08:20:05

Message: 12 of 13

Two notes,

before applying ACUMARRAY, preprocess the data with UNIQUE so as to create smallest possible array (and without bother with NaN as Ross's original method). The second advantage is it can work when grouping is based on floating, negative numbers, etc...

 A = [1 1 150; 1 1 125; 1 1 130; 1 2 160; 1 2 130; 6 1 250; 6 1 98; 6 2 210];

[U I ID] = unique(A(:,1:2),'rows');
m = accumarray(ID,A(:,3),[],@median);
[U m]

Bruno

Subject: simple question and difficult in the same time

From: Ross W

Date: 2 Nov, 2010 08:33:08

Message: 13 of 13

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <iaohjl$dgc$1@fred.mathworks.com>...
> Two notes,
>
> before applying ACUMARRAY, preprocess the data with UNIQUE so as to create smallest possible array (and without bother with NaN as Ross's original method). The second advantage is it can work when grouping is based on floating, negative numbers, etc...
>
> A = [1 1 150; 1 1 125; 1 1 130; 1 2 160; 1 2 130; 6 1 250; 6 1 98; 6 2 210];
>
> [U I ID] = unique(A(:,1:2),'rows');
> m = accumarray(ID,A(:,3),[],@median);
> [U m]
>
> Bruno

Very nice Bruno!

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