Calculate a return (base index)

6 views (last 30 days)
Aleksandar
Aleksandar on 30 Nov 2014
Commented: Aleksandar on 1 Dec 2014
I would like to calculate the column Base Index , for each particular ID is id(i)/id(base)*100, where id(base) is the first item FOR EACH id
ID Value Base Index
1 10 100
1 15 150 (e.g.15/10*100)
1 4 40
2 3 100
2 20 667
2 8 267
2 11 367
3 25 100
with this code I only get right answers for first id...how can I deal with the last one?
bvalue(1)=100;
for i=2:length(id)-1
if id(i)~=id(i-1) & id(i)==id(i+1)
bvalue(i)=100;
else
bvalue(i)=value(i)/value(id(i))*100;
end
end

Accepted Answer

the cyclist
the cyclist on 1 Dec 2014
There were a couple small errors. I think I fixed them.
id = [1 1 1 2 2 2 2 3];
value = [10 15 4 3 20 8 11 25];
base = value(1);
bvalue(1)=100;
for i=2:length(id)
if id(i)~=id(i-1)
bvalue(i)=100;
base = value(i);
else
bvalue(i)=value(i)/base*100;
end
end

More Answers (0)

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!