Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB insert value in between

Tags:

vector

matlab

in MATLAB i wish to insert a value half way between every element in the vector

for example

  v=[1,3,5,7,9]

i want to get

  v=[1,2,3,4,5,6,7,8,9]

is there a quick way to do this?

like image 431
Robert Dennis Avatar asked Mar 19 '11 21:03

Robert Dennis


2 Answers

A very simple, general way to do this is with interpolation, specifically the function INTERP1:

>> v = [1 3 5 7 9]

v =

     1     3     5     7     9

>> v = interp1(v,1:0.5:numel(v))

v =

     1     2     3     4     5     6     7     8     9
like image 58
gnovice Avatar answered Nov 15 '22 05:11

gnovice


a = [1 3 5 7 9];
b = [2 4 6 8];
c = zeros(9,1);
c(1:2:9) = a; c(2:2:8) = b;
like image 23
George Avatar answered Nov 15 '22 06:11

George