Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Getting values in between the values of a vector?

Subject: Getting values in between the values of a vector?

From: Brandon Rodriguez

Date: 19 Apr, 2009 18:06:01

Message: 1 of 2

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!

Subject: Getting values in between the values of a vector?

From: Jiro Doke

Date: 19 Apr, 2009 18:25:03

Message: 2 of 2

"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

Tags for this Thread

No tags are associated with this thread.

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.

Contact us