Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Getting values in between the values of a vector?
Date: Sun, 19 Apr 2009 18:25:03 +0000 (UTC)
Organization: The MathWorks Inc
Lines: 23
Message-ID: <gsfq9v$c3e$1@fred.mathworks.com>
References: <gsfp69$19b$1@fred.mathworks.com>
Reply-To: <HIDDEN>
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 1240165503 12398 172.30.248.35 (19 Apr 2009 18:25:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 19 Apr 2009 18:25:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869871
Xref: news.mathworks.com comp.soft-sys.matlab:533886


"Brandon Rodriguez" <coasters2k@yahoo.com> wrote in message <gsfp69$19b$1@fred.mathworks.com>...
> Say I have a vector that's like this:
> 
> [-6 -3 0 3 6]
> 
> How can I get it get the number in between each of the two values to return something like:
> 
> [-4.5 -1.5 1.5 4.5]
> 
> Is there a built-in function that can do this? I'm asking because I'd like to avoid a loop if possible (but I'm thinking it's the only way). Thanks!

Here are a couple of ways to do it:

>> a = [-6 -3 0 3 6];
>> diff(a)/2 + a(1:end-1)
ans =
         -4.5         -1.5          1.5          4.5

OR

>> (a(1:end-1) + a(2:end))/2
ans =
         -4.5         -1.5          1.5          4.5