fprintf into a stored variable
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Instead of using fprintf I want to store all the i, xe, and f(xe) into a vector instead. That way I can call them later. Also I had another problem where I wanted to use another 'i' later on in the code. But it only allowed me to use 1. How can you use two different 'i's. So they don't get mixed up.
f = @(x) x^2-6;
xl = 0;
xu = 10;
xe = (xu+xl)/(2);
acc = 0.00001;
while abs(f(xe))> acc
i = i + 1;
fprintf('%d)\t%f\tf(x)=%f\n',i,xe,f(xe));
if (f(xe)*f(xl)) < 0
xu = xe;
else
xl = xe;
end
xe = (xu+xl)/2;
end
採用された回答
per isakson
2016 年 2 月 13 日
編集済み: per isakson
2016 年 2 月 13 日
Add
M = zeros( 0, 2 );
before the loop and replace
fprintf('%d)\t%f\tf(x)=%f\n',i,xe,f(xe));
by
M( end+1, 1:2 ) = [ xe, f(xe) ];
You'll get a warning about "Growing ..." , but if speed is acceptable ignore it.
 
"fprintf into a stored variable"   If understood literally, replace
fprintf('%d)\t%f\tf(x)=%f\n',i,xe,f(xe));
by
C{end+1,1} = sprintf('%d)\t%f\tf(x)=%f\n',i,xe,f(xe));
and add C=cell(0,1) before the loop
10 件のコメント
WhatIsMatlab-
2016 年 2 月 13 日
Thank you for the reply. I will try this and see what happens.
if speed is acceptable ignore it.
Speed will decrease the longer the loop goes on (more and more elements need to be copied to a new memory location). If the loop converges fast enough, then indeed speed should not be an issue.
To stop the warning from appearing altogether:
M( end+1, 1:2 ) = [ xe, f(xe) ]; %#ok<AGROW>
per isakson
2016 年 2 月 13 日
編集済み: per isakson
2016 年 2 月 13 日
Welcome point.
On the FEX there is a solution and good discussion on the problem of pre-allocation when the final size is not known: Incremental growth of an array, revisited, by John D'Errico
WhatIsMatlab-
2016 年 2 月 14 日
How do I stop it from using the same 'i' for each while loop? I want it to start counting from zero for the second while loop
First While Loop
s = zeros( 0 );
while abs(f(xe))> acc
i = i + 1;
s( end+1, 1:1 ) = [xe];
if (f(xe)*f(xl)) < 0
xu = xe;
else
xl = xe;
end
xe = (xu+xl)/2;
end
Second While Loop
m = zeros( 0 );
while abs(ff(xxe))> aacc
i = i + 1;
m( end+1, 1:1 ) = [xxe];
if (ff(xxe)*ff(xxl)) < 0
xxu = xxe;
else
xxl = xxe;
end
xxe = xxu - ((ff(xxu)*(xxl-xxu))/(ff(xxl)-ff(xxu)));
end
per isakson
2016 年 2 月 14 日
編集済み: per isakson
2016 年 2 月 14 日
- "How do I stop it from using the same 'i' for each while loop"   Do you mean the same i in the first and second while loop? In the code you show i is incremented but not used.
- Did you inspect the values of m and s after running the code? These values are they not the ones you expect?
WhatIsMatlab-
2016 年 2 月 14 日
編集済み: WhatIsMatlab-
2016 年 2 月 14 日
I was just trying to use the 'i' to count my iterations if there is a better way of achieving this maybe you can help me.
- I was using the 'i' to count my iterations for the problem. I may be doing that incorrectly. Yes, I was referring to the 'i' in the first and second while loop.
- They are the values I expect but the iterations are incorrect because the second while loop is starting from the last 'i' value of the first loop.
Thanks
Guillaume
2016 年 2 月 14 日
Well, set i to 0 before the 2nd loop, or use a different variable.
WhatIsMatlab-
2016 年 2 月 14 日
Setting 'i=0' before the loops worked. I tried changing the variable but I got an error. Thank you for taking the time to figure this problem out with me.
per isakson
2016 年 2 月 15 日
- "but I got an error"   we cannot help if you don't show what you did and what error Matlab reported.
- "a better way of achieving this"   length(s) and length(m) tell the number of iterations in the two loops.
per isakson
2016 年 2 月 15 日
This example illustrates
- two ways to count the number of loop iterations
- two ways to store result in the loop
N = 1e6;
M1 = zeros( 0 );
ii = 0;
M2 = nan( N, 1 ); % Pre-allocate
while sqrt(ii) < sqrt(N) - 10
ii = ii + 1;
M1( end+1, 1 ) = ii;
M2(ii) = ii;
end
M2(isnan(M2)) = [];
disp( all( M2 == M1 ) )
disp( ii )
disp( length(M1) )
disp( length(M2) )
outputs
1
980100
980100
980100
where "1" shows that M1 and M2 are identical.
[Run and Time], i.e. the profiler, shows that "M1" is slower.

その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
製品
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
