Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Elementary Row Operations !

Subject: Elementary Row Operations !

From: alan dinno

Date: 28 Dec, 2007 22:38:53

Message: 1 of 4

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

Subject: Re: Elementary Row Operations !

From: Roger Stafford

Date: 28 Dec, 2007 22:55:06

Message: 2 of 4

"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

Subject: Re: Elementary Row Operations !

From: Roger Stafford

Date: 28 Dec, 2007 23:15:53

Message: 3 of 4

"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

Subject: Re: Elementary Row Operations !

From: alan dinno

Date: 28 Dec, 2007 23:35:48

Message: 4 of 4

"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

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics