# How do you add elements in a matrix (single column, multiple rows) and output to another matrix?

33 ビュー (過去 30 日間)
James Long 2017 年 4 月 18 日
コメント済み: James Long 2017 年 4 月 18 日
For Example, I have the following:
A1= [1 2 3] (where A1 is a single vertical column, not a single row as it appears here) or in the general sense, A2= [n n+1 n+2] (where A2 is a single vertical column, not a single row as it appears here)
and what I want to do is produce a B such that B= [n n+1-(n) n+2-(n+1)] where the specific element in the matrix is just a subtraction operation between the original corresponding element, and the element directly above it.

#### 1 件のコメント

Stephen Cobeldick 2017 年 4 月 18 日
What would you expect the output to be, for A = [1;2;3] ?

サインイン to comment.

### 採用された回答

Stephen Cobeldick 2017 年 4 月 18 日

Use diff:
>> [A(1);diff(A)]
ans =
1
1
1

#### 1 件のコメント

James Long 2017 年 4 月 18 日
worked perfectly! thanks Stephen.

サインイン to comment.

### その他の回答 (1 件)

xi 2017 年 4 月 18 日
First of all, A1= [1 2 3] , A1 is one row, not one column. to make it a column, should write A1= [1; 2; 3]
then, try this ------------- A1= [1; 2; 3] B=[A1;0]-[0;A1] B=B(1:end-1) %remove the last element in B

#### 0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

Translated by