f



Getting values in between the values of a vector?

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!
0
4/19/2009 6:06:01 PM
comp.soft-sys.matlab 211266 articles. 23 followers. lunamoonmoon (257) is leader. Post Follow

1 Replies
1219 Views

Similar Articles

[PageSpeed] 33

"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
0
jiro.doke1 (127)
4/19/2009 6:25:03 PM
Reply: