How to create column that is the difference between rows in another column
Afficher commentaires plus anciens
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
Adam
le 7 Juil 2017
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.
Réponses (1)
Adam
le 7 Juil 2017
doc diff
3 commentaires
owen kelly
le 7 Juil 2017
Guillaume
le 7 Juil 2017
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];
owen kelly
le 7 Juil 2017
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!