Subtract each number from each other in vector

2 vues (au cours des 30 derniers jours)
Confidential Confidential
Confidential Confidential le 31 Jan 2019
I'm trying to get a vector to subtract the value from the one to the right of it. I'm not sure how to do this with masking/indexing to select the right values. Say I have a vector like this [1, 7, 8, 9, 6] I want to get it where each value is subtracted from the next values on the right so it'd be like (1-7) (7-8) (8-9) (9-6) and place it into a new vector. It'd also have to work with an even number of entries so it'd have to be very generic and not specifically set to any length.

Réponse acceptée

madhan ravi
madhan ravi le 31 Jan 2019
Modifié(e) : madhan ravi le 31 Jan 2019
new_vector=-diff(vector)
%or
new_vector=vector(1:end-1)-vector(2:end)
  1 commentaire
Confidential Confidential
Confidential Confidential le 31 Jan 2019
Thank you...glad there is a command because indexing seems difficult with varying lengths.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Argument Definitions dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by