将行插入矩阵matlab

时间:2010-09-05 18:14:35

标签: matlab insert row

我有一个~100000/2矩阵。我想沿着列向下,平均每个垂直相邻的值,并在两个值之间插入该值。例如......

1  2
3  4
4  6
7  8

会变成

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

我不确定在matlab中是否有简洁的方法可以做到这一点。我看了一下http://www.mathworks.com/matlabcentral/fileexchange/9984,但它似乎将矩阵中的所有行插入到特定点的另一行中。显然它仍然可以使用,但只是想知道是否有更简单的方法。

感谢任何帮助,谢谢。

3 个答案:

答案 0 :(得分:2)

未测试:

% 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]);

答案 1 :(得分:1)

%# 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;

答案 2 :(得分:0)

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
相关问题