Insert rows in matlab matrix

I have a matrix ~ 100000/2. I would like to go down the columns, average each adjacent value vertically and insert this value between the two values. For instance...

1 2 3 4 4 6 7 8 

will become

 1 2 2 3 3 4 3.5 5 4 6 5.5 7 7 8 

I'm not sure if Matlab has a short way to do this. I took a look at http://www.mathworks.com/matlabcentral/fileexchange/9984 , but it seems to insert all the rows in the matrix to another at a specific point. Obviously, it can still be used, but just wondering if there is an easier way.

Any help is appreciated, thanks.

+4
source share
3 answers

Unverified:

 % Take the mean of adjacent pairs x_mean = ([x; 0 0] + [0 0; x]) / 2; % Interleave the two matrices y = kron(x, [1;0]) + kron(x_mean(1:end-1,:), [0;1]); 
+2
source
 %# works for any 2D matrix of size N-by-M X = rand(100,2); adjMean = mean(cat(3, X(1:end-1,:), X(2:end,:)), 3); Y = zeros(2*size(X,1)-1, size(X,2)); Y(1:2:end,:) = X; Y(2:2:end,:) = adjMean; 
+1
source
 octave-3.0.3:57> a = [1,2; 3,4; 4,6; 7,8] a = 1 2 3 4 4 6 7 8 octave-3.0.3:58> b = (circshift(a, -1) + a) / 2 b = 2.0000 3.0000 3.5000 5.0000 5.5000 7.0000 4.0000 5.0000 octave-3.0.3:60> reshape(vertcat(a', b'), 2, [])'(1:end-1, :) ans = 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 3.5000 5.0000 4.0000 6.0000 5.5000 7.0000 7.0000 8.0000 
0
source

All Articles