Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1998

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

Search the Archive

RE: sort and find in MATHEMATI

A. Jerschow  wrote:
|in MATLAB I would write:
|to sort the matrix a by it's i-th column. |
|What's the easiest way to do the same in MATHEMATICA ? |

First we make a matrix of random integers called (data). In[1]:=


Note:  If you use TableForm[data]   or  MatrixForm[data]  the data are 
displayed as a Table or a Matrix.  The two forms are very similar.

In the line below we sort the data on the second column. In general we
use OrderedQ[{#1[[n]], #2[[n]]}]&  to Sort on column (n). In[2]:=


The Mathematica syntax for this is a bit cryptic. Let me know if you
want me to explain more about this (#1[[2]], #2[[2]])  business.
Excuse me for my lack of modesty , but I think I have a better
explanation  than anything else I have found.

A novice might be tempted to do this using a Do loop or For loop, etc.
But that is much less efficient using Mathematica.

|Another thing, again MATLAB:
|index=find(a(:,i) == 0);
|to remove lines, which have zeros in the i-th column. |

The following removes all data that have zero for the second element.
Select[data,(#[[2]]!=0)& ]


In the following lines I show you how to do some other things that are
very  useful.

We can perform an operation f[] on the third column of data. In[4]:=


We can swap the second and third columns. In[5]:=


That's all for now.
Ted Ersek

  • Prev by Date: Global Optimization - New product announcement
  • Next by Date: Re: Re: Get rid of Input cells when printing?
  • Prev by thread: Global Optimization - New product announcement
  • Next by thread: [CFP] Int. SSCC98 Conf., 22-24 Sep. 1998, Durban, South Africa