Re: sort and find in MATHEMATICA like in MATLAB
- To: mathgroup@smc.vnet.net
- Subject: [mg11035] Re: sort and find in MATHEMATICA like in MATLAB
- From: Paul Abbott <paul@physics.uwa.edu.au>
- Date: Wed, 18 Feb 1998 20:31:34 -0500
- Organization: University of Western Australia
- References: <6catkj$fsn@smc.vnet.net>
Alexej Jerschow wrote: > in MATLAB I would write: > > [dummy,index]=sort(a(:,i)); > sorta=a(index,:); > > to sort the matrix a by it's i-th column. > > What's the easiest way to do the same in MATHEMATICA ? In[1]:= a=Table[Random[Integer,{0,4}],{4},{4}] Out[1]= {{4, 3, 3, 1}, {0, 1, 4, 4}, {4, 3, 4, 3}, {4, 0, 4, 3}} Sort by the 2nd column: In[2]:= Sort[a,#1[[2]]<#2[[2]]&] Out[2]= {{4, 0, 4, 3}, {0, 1, 4, 4}, {4, 3, 4, 3}, {4, 3, 3, 1}} > Another thing, again MATLAB: > > index=find(a(:,i) == 0); > sorta=a(index,:); > > to remove lines, which have zeros in the i-th column. Remove rows with zeros in the 2nd column: In[3]:= Select[a, #[[2]]=!=0&] Out[3]= {{4, 3, 3, 1}, {0, 1, 4, 4}, {4, 3, 4, 3}}> Cheers, Paul ____________________________________________________________________ Paul Abbott Phone: +61-8-9380-2734 Department of Physics Fax: +61-8-9380-1014 The University of Western Australia Nedlands WA 6907 mailto:paul@physics.uwa.edu.au AUSTRALIA http://www.pd.uwa.edu.au/~paul God IS a weakly left-handed dice player ____________________________________________________________________