Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!flph199.ffdc.sbc.com!prodigy.com!flpi107.ffdc.sbc.com!nlpi069.nbdc.sbc.com.POSTED!ffbda4aa!not-for-mail
Reply-To: "Nasser Abbasi" <nma@12000.org>
From: "Nasser Abbasi" <nma@12000.org>
Newsgroups: comp.soft-sys.matlab
References: <gp44d7$bgd$1@fred.mathworks.com>
Subject: Re: Avoiding for loop
Lines: 44
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350
X-RFC2646: Format=Flowed; Original
Message-ID: <ehmtl.15384$as4.12507@nlpi069.nbdc.sbc.com>
NNTP-Posting-Host: 75.38.6.108
X-Complaints-To: abuse@prodigy.net
X-Trace: nlpi069.nbdc.sbc.com 1236660426 ST000 75.38.6.108 (Tue, 10 Mar 2009 00:47:06 EDT)
NNTP-Posting-Date: Tue, 10 Mar 2009 00:47:06 EDT
Organization: at&t http://my.att.net/
X-UserInfo1: OXZUSWKOG@C_GFLYZBNXKQ@@AZJLCQLIQQ]ZMVMHQAVTUZ]CLNTCPFK[WDXDHV[K^FCGJCJLPF_D_NCC@FUG^Q\DINVAXSLIFXYJSSCCALP@PB@\OS@BITWAH\CQZKJMMD^SJA^NXA\GVLSRBD^M_NW_F[YLVTWIGAXAQBOATKBBQRXECDFDMQ\DZFUE@\JM
Date: Mon, 9 Mar 2009 21:47:01 -0700
Xref: news.mathworks.com comp.soft-sys.matlab:523647



"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