Thread Subject: Avoiding for loop

Subject: Avoiding for loop

From: Pete sherer

Date: 9 Mar, 2009 22:15:03

Message: 1 of 5

Are there anyway I can avoid using FOR loop for the case shown below?

oriX = [1 2 3 4 5
10 20 30 40 50
100 200 300 400 500
1000 2000 3000 4000 5000];
rowindx = [2 4 1 3 3];
newX= zeros( 1, length(rowindx));

for ii = 1: 5
newX(ii) = oriX( rowindx(ii), ii);
end


newX =

          10 2000 3 400 500

Thanks a lot in advance

Subject: Avoiding for loop

From: Matt Fig

Date: 9 Mar, 2009 22:23:01

Message: 2 of 5

[r,c] = size(oriX)
oriX(sub2ind([r,c],rowindx,1:c))




alssg`&`n?`&n~nmsfhjunxa~nk9d`g~~~dn`mdk~XhdLDb~ogl~o`tt-`b

Subject: Avoiding for loop

From: Nasser Abbasi

Date: 10 Mar, 2009 04:47:01

Message: 3 of 5


"Pete sherer" <tsh@abg.com> wrote in message
news:gp44d7$bgd$1@fred.mathworks.com...
> Are there anyway I can avoid using FOR loop for the case shown below?
>
> oriX = [1 2 3 4 5
> 10 20 30 40 50
> 100 200 300 400 500
> 1000 2000 3000 4000 5000];
> rowindx = [2 4 1 3 3];
> newX= zeros( 1, length(rowindx));
>
> for ii = 1: 5
> newX(ii) = oriX( rowindx(ii), ii);
> end
>
>
> newX =
>
> 10 2000 3 400 500
>
> Thanks a lot in advance

Matt just gave you a solution.

But, I just want to say something here.

Unless there is a huge performance difference (which might well be the
case), I would prefer to see the loop solution.

The loop solution is more clear, and someone who is not a matlab expert can
understand it more easily.

Also, in the future, in the unforeseen event that one wants to 'port' the
code to a different language, it is much easier to convert standard loop
construct as those are standard in many languages.

All what I am saying is, unless performance is a big issue, stick with the
more easy to understand constructs for the reader, even thought it might
take more key strokes to write it.

--Nasser

Subject: Avoiding for loop

From: per isakson

Date: 10 Mar, 2009 06:55:03

Message: 4 of 5

"Nasser Abbasi" <nma@12000.org> wrote in message <ehmtl.15384$as4.12507@nlpi069.nbdc.sbc.com>...
>
> "Pete sherer" <tsh@abg.com> wrote in message
> news:gp44d7$bgd$1@fred.mathworks.com...
> > Are there anyway I can avoid using FOR loop for the case shown below?
> >
> > oriX = [1 2 3 4 5
> > 10 20 30 40 50
> > 100 200 300 400 500
> > 1000 2000 3000 4000 5000];
> > rowindx = [2 4 1 3 3];
> > newX= zeros( 1, length(rowindx));
> >
> > for ii = 1: 5
> > newX(ii) = oriX( rowindx(ii), ii);
> > end
> >
> >
> > newX =
> >
> > 10 2000 3 400 500
> >
> > Thanks a lot in advance
>
> Matt just gave you a solution.
>
> But, I just want to say something here.
>
> Unless there is a huge performance difference (which might well be the
> case), I would prefer to see the loop solution.
>
> The loop solution is more clear, and someone who is not a matlab expert can
> understand it more easily.
>
> Also, in the future, in the unforeseen event that one wants to 'port' the
> code to a different language, it is much easier to convert standard loop
> construct as those are standard in many languages.
>
> All what I am saying is, unless performance is a big issue, stick with the
> more easy to understand constructs for the reader, even thought it might
> take more key strokes to write it.
>
> --Nasser
>

The loop is an order of magnitude fastern than the proposed one-liner on my PC
/per

Subject: Avoiding for loop

From: Matt Fig

Date: 11 Mar, 2009 03:04:02

Message: 5 of 5

"per isakson" <poi.nospam@bimDOTkthDOT.se> wrote in message
> The loop is an order of magnitude fastern than the proposed one-liner on my PC
> /per


As is so often the case. In this particular case the for loop is hidden in sub2ind mcode. So there is the function call overhead and error checking overhead, etc.

I wonder how many releases of Matlab it will take until there are not so many requests to get rid of the 'evil' For loops. It seems to me that there is still a lingering perception that For loops in Matlab MUST be slow. This, of course was understandable prior to 6.5. Since then, maybe not so much...






GHRGA4HA5:RC@<R?6,4RB84B5B@Y8C4L? 8YIG;BR;w;844l<RRRBB4>`r6

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

Contact us at files@mathworks.com