Re: sort and find in MATHEMATICA like in MATLAB
- To: mathgroup@smc.vnet.net
- Subject: [mg11045] Re: sort and find in MATHEMATICA like in MATLAB
- From: Allan Hayes <hay@haystack.demon.co.uk>
- Date: Wed, 18 Feb 1998 20:31:45 -0500
- 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 ? > > Another thing, again MATLAB: > > index=find(a(:,i) == 0); > sorta=a(index,:); > > to remove lines, which have zeros in the i-th column. I'll use $PrePrint = MatrixForm; so that matrices are formatted (I'm also using menu>Cell>Default Output FormatType > OutputForm to get it in ASSCII) I can't guarantee that these are the easiest ways but: taking m = {{5,3,1},{7,0,2},{3,2,5}} 5 3 1 7 0 2 3 2 5 and i = 2: Sort[m, #1[[2]]<=#2[[2]]&] 7 0 2 3 2 5 5 3 1 DeleteCases[m,r_/;r[[2]]==0] 5 3 1 3 2 5 -- Allan Hayes Training and Consulting Leicester, UK hay@haystack.demon.co.uk http://www.haystack.demon.co.uk voice: +44 (0)116 271 4198 fax: +44 (0)116 271 8642