How to create column that is the difference between rows in another column

I have uploaded an excel file and I am trying find A way to create a new column in it that takes the 1st column and calculates the difference between the rows. An Say if column 1 had rows 5-6-8-9..column 2 would read 1-2-2-1
Any suggestions

1 commentaire

Surely the result here should be 1-2-1?
You will always have 1 fewer value than in the rows you started with since it is an operation on two rows.

Connectez-vous pour commenter.

Réponses (1)

doc diff

3 commentaires

I tried that but it wont allow me to make a new column. my column is named temps(:,1)
for the second column I tried temps(:,2)=diff(temps(:,1))
it won't allow me to make a new column
There's no won't allow me to make a new column. You get a specific error message that tells you what the problem is. If you need help with an error message, then you need to tell us the exact full text of the error.
In your case, I suspect the error is Subscripted assignment dimension mismatch. As Adam commented in your question. The difference between rows will have one element less than the number of rows. Of course, since matrices must have the same number of elements in all rows (and columns), you can't create a column with less element. That is what matlab won't allow.
A possible workaround:
temps(:, 2) = [diff(temps(:, 1)); NaN];
I know there are error messages but currently no errors are showing. I managed to resolve the problem by adding another row (in a different matrix) and then inserting the column into the matrix. Thank you

Connectez-vous pour commenter.

Catégories

Produits

Commenté :

le 7 Juil 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by