Vectorizing Code Yields Different Answer to For-Loop

I ended up quickly writing a piece of code that theoretically should plot one value for each plot, giving 10 of them in total. It does do this. What if I wanted to remove the for-loop? I could vectorize the code simply by removing the for-loop, but then this plots a single plot with all the values upon it. Is there a way for me to remove the for-loop, but have my program plot a single value for each of the 10 iterations, yielding 10 plots as before?
cd('~/Documents/MATLAB/plots/test')
for x = 1:1:10;
y = x+2;
plot(x,y,'*')
filename = strcat('x_',num2str(x));
print(gcf,'-painters','-dpdf',strcat(filename,'.pdf'));
end

3 件のコメント

Geoff Hayes
Geoff Hayes 2017 年 10 月 31 日
BMor - why vectorize your code if you then want to plot each value individually? Is it because you still want to save each plot to file?
BM
BM 2017 年 10 月 31 日
Yes, in fact this test code I wrote mimics what I want to do for a much more complex program. If I kept the for-loop in my other program, I would have 150-200 lines of code between it, some of which already contain for-loops. I am literally trying to experiment with this code to see if there is a simple way of making this run faster and more efficient, so that when I use the same technique on my other program, it should save some time.
BM
BM 2017 年 10 月 31 日
My other program will need to have separate plots for each run, as I vary a specific value. I am interested in how that value changes the result when it varies.

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

BM
2017 年 10 月 31 日

コメント済み:

BM
2017 年 10 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by