Appending vector in long for-loop

2 ビュー (過去 30 日間)
Jonathan
Jonathan 2013 年 11 月 8 日
編集済み: Azzi Abdelmalek 2013 年 11 月 11 日
I have this loop where x = 62012:
while y < x
p1 = percentChange(avgLine(y-9), avgLine(y-8));
p2 = percentChange(avgLine(y-9), avgLine(y-7));
p3 = percentChange(avgLine(y-9), avgLine(y-6));
p4 = percentChange(avgLine(y-9), avgLine(y-5));
p5 = percentChange(avgLine(y-9), avgLine(y-4));
p6 = percentChange(avgLine(y-9), avgLine(y-3));
p7 = percentChange(avgLine(y-9), avgLine(y-2));
p8 = percentChange(avgLine(y-9), avgLine(y-1));
p9 = percentChange(avgLine(y-9), avgLine(y-0));
p10 = percentChange(avgLine(y-9), avgLine(y+1));
During this loop, I want to create a vector: [p1 p2 p3 ... p10] and after every iteration, there would be 10 new elements added to the end of it. Can I do this efficiently without using 'append'?
Thanks in advance

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 8 日
編集済み: Azzi Abdelmalek 2013 年 11 月 8 日
out=zeros(1,62012*10);
n=0;
while y < x
n=n+1;
for k=1:10
out(1,k+(n-1)*10)= percentChange(avgLine(y-9), avgLine(y+k-9));
end
end
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 9 日
We don't know how y varies, how your function percentChange works.
Jonathan
Jonathan 2013 年 11 月 11 日
編集済み: Azzi Abdelmalek 2013 年 11 月 11 日
function [ x ] = percentChange( startPoint, currentPoint )
format long
x = ((currentPoint - startPoint)/abs(startPoint))*100.00;
if x==0
x = 0.00000000001;
end
After each loop, y = y+1;

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by