Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Operation on specific cases' topicStudent Support Forum > General > Archives > "Operation on specific cases"

< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic
Author Comment/Response
Bill Simpson
02/26/13 00:26am

In[1]:= a={{1,1,1,2,2,2},{2,4,3,4,5,3}};
at=Transpose[a];
ad=DeleteDuplicates[First[a]];
al=Map[Cases[at,{#,v_}->v]&,ad];
Map[Mean,al]

Out[5]= {3,4}

That should work fine, but if your input might ever be something like

a={{1,1,2,2,1,1},{2,4,3,4,5,3}}

and you expect the output to be

{3,7/2,4}

then DeleteDuplicates may not be the function to use. If that could ever happen then this might work.

In[7]:= a={{1,1,2,2,1,1},{2,4,3,4,5,3}};
at=Transpose[a];
as=Split[at,First[#1]==First[#2]&];
Map[Last,Map[Mean,as]]

Out[10]= {3, 7/2, 4}


URL: ,

Subject (listing for 'Operation on specific cases')
Author Date Posted
Operation on specific cases Amin 02/25/13 3:08pm
Re: Operation on specific cases yehuda 02/25/13 11:37pm
Re: Operation on specific cases Bill Simpson 02/26/13 00:26am
Re: Re: Operation on specific cases yehuda 02/27/13 02:12am
Re: Re: Re: Operation on specific cases Amin 02/28/13 4:53pm
Re: Re: Re: Re: Operation on specific cases yehuda 02/28/13 10:51pm
Re: Operation on specific cases Amin 03/01/13 10:33pm
< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic