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

 採用された回答

the cyclist
the cyclist 2014 年 12 月 1 日

0 投票

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

1 件のコメント

Aleksandar
Aleksandar 2014 年 12 月 1 日
thanks a lot!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2014 年 11 月 30 日

コメント済み:

2014 年 12 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by