1

I have a vector A= [4 7 10] what I want to do is to sum every two elements and put the result in a new vector. So for example vector B= [11,17] which is the the sum of 4+7 and 7+10.

So if anyone could advise me how can I do this without loops.

4

3 回答 3

5

In my view it is:

B = A(1:end-1) + A(2:end);
于 2013-08-15T09:20:13.000 回答
4

如果您想在将来添加 3 或 4 组等,这里有一个很容易概括的替代方法:

n = 2
conv(A, ones(1, n), 'valid')
于 2013-08-15T09:29:43.303 回答
-1

你可以这样做

B = A(:,1:end-1) + A(:,2:end);

此代码不仅限于行向量。它也适用于 MxN 矩阵。

于 2013-08-15T09:06:58.993 回答