How to make a function that calculates percentage change in a vector

24 views (last 30 days)
i have a document called gdp.csv which contains a vertical vector of values. How would I make a script that returns the percentage change between every subsequent value?
x=csvread('gdp.csv')
for i=0; i++; i == length(x)-1
p = (x(i+1)-x(i))/(x(i))
endfor

Answers (1)

KSSV
KSSV on 3 Apr 2020
Loop:
x=csvread('gdp.csv')
for i=1:length(x)-1
p = (x(i+1)-x(i))/(x(i))
end
Vector:
p = diff(x)./x(1:end-1) ;

Tags

Community Treasure Hunt

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

Start Hunting!