Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Reading from beginning and end of an array and repeat
Date: Sun, 22 Feb 2009 17:17:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 35
Message-ID: <gns1ae$dk5$1@fred.mathworks.com>
References: <33098052.1235314424604.JavaMail.jakarta@nitrogen.mathforum.org>
Reply-To: <HIDDEN>
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 1235323022 13957 172.30.248.37 (22 Feb 2009 17:17:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 22 Feb 2009 17:17:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:520012

enviro <farhadnejadkoorki@yahoo.co.uk> wrote in message <33098052.1235314424604.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hello everyone
> I want to select an array(s)with length n out of
> a bigger array (z) in a way that the 
> s =[162 320 169 280 172]' 
> or(get one from first row and one from end and repeat until it reaches n)
> How to  write a code to get array s? 
> below is what I tried.
> 
> z=[162   169   172   184   199   205 220 240 280 320]'
> n=5;
> [p,q]=size(z);
> s=zeros(n,1);
> [d,e]=size(s);
> 
> 
> for i=1:p;
>     for j=p:p-(i-1);
>         for d=1:n;
>             for f=d+1:n;
>                 s(d)=z(i); 
>                 s(f)=z(j);
>             end
>         end
>     end
> end
> s

  Do this:

 n = 5;
 m = size(z,2);
 s = z((2*m+3+(2*m-2*(1:n)+1).*(-1).^(1:n))/4);

Roger Stafford