Path: news.mathworks.com!not-for-mail
From: "alan dinno" <alan_dinno@hotmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Elementary Row Operations !
Date: Fri, 28 Dec 2007 23:35:48 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 61
Message-ID: <fl418k$d4a$1@fred.mathworks.com>
References: <fl3ttt$nce$1@fred.mathworks.com> <fl3us9$ant$1@fred.mathworks.com> <fl4039$de8$1@fred.mathworks.com>
Reply-To: "alan dinno" <alan_dinno@hotmail.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1198884948 13450 172.30.248.37 (28 Dec 2007 23:35:48 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 28 Dec 2007 23:35:48 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1192926
Xref: news.mathworks.com comp.soft-sys.matlab:443767


"Roger Stafford" 
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in 
message <fl4039$de8$1@fred.mathworks.com>...
> "Roger Stafford" 
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in 
> message <fl3us9$ant$1@fred.mathworks.com>...
> > "alan dinno" <alan_dinno@hotmail.com> wrote in message 
<fl3ttt$nce
> > $1@fred.mathworks.com>...
> > > Hi -
> > > 
> > > If I wanted to loop through column k in matrix A, 
and swap 
> > > rows with zeros with rows without zeros, is there a 
faster 
> > > way to achieve this than using multiple loops?
> > > 
> > > Example: If A=[ 20 23 5  8
> > >                 92 0  16 14
> > >                 60 0  35 88
> > >                 44 25 83 19
> > >                 44 76 74 20]
> > > 
> > > Then if I want to operate on column 2 (i.e k=2),the 
new A 
> > > will should look like:
> > >              A=[ 20 23 5  8
> > >                  44 25 83 19
> > >                  44 76 74 20
> > >                  92 0  16 14 
> > >                  60 0  35 88]
> > > 
> > > That is, the new A is the same except rows 2 and 3 
of 
> > > column 2 were interchanged with rows 4 and 5 of 
column 2 
> > > because the former contained zeros.
> > > 
> > > Thanks
> > > Alan
> > ------
> > Try this:
> > 
> >   [ignore,p] = sort(A(:,k)==0);
> >  A = A(p,:);
> > 
> > Roger Stafford
> -------
> Or you could avoid the sort with:
> 
>  A = A([find(A(:,k)~=0);find(A(:,k)==0)],:);
> 
> Roger Stafford
> 

This is excellent, Roger! 
I like the one without the sort() function...it is more 
efficient for me.

Thanks.
Alan