MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: sort and find in MATHEMATICA like in MATLAB



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




  • Prev by Date: Re: visual subscripts with assignment
  • Next by Date: Complex Number problem
  • Prev by thread: sort and find in MATHEMATICA like in MATLAB
  • Next by thread: Re: sort and find in MATHEMATICA like in MATLAB