Path: news.mathworks.com!not-for-mail
From: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Elementary Row Operations !
Date: Fri, 28 Dec 2007 23:15:53 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 44
Message-ID: <fl4039$de8$1@fred.mathworks.com>
References: <fl3ttt$nce$1@fred.mathworks.com> <fl3us9$ant$1@fred.mathworks.com>
Reply-To: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1198883753 13768 172.30.248.35 (28 Dec 2007 23:15:53 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 28 Dec 2007 23:15:53 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:443765


"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