フィルターのクリア

Storing values from a for loop

2 ビュー (過去 30 日間)
Hamzah Mahmood
Hamzah Mahmood 2020 年 8 月 23 日
コメント済み: Alan Stevens 2020 年 8 月 23 日
HI,
I've noticed this question appears a lot, and I watched the video here :https://www.mathworks.com/videos/storing-data-in-a-matrix-from-a-loop-97493.html, but I haven't been able to figure it out for my own code. The code outputs a value for the area, however doesn't store it as a variable. Any suggestions?
clc;
clear;
x = [ 0 1 2 3 4 5 6 7 8 9 10 11 12];
y = [0 -0.5 -0.8 -0.8 -1 -1.1 -1.2 -1.2 -1.4 -1.2 -1.1 -1 0];
for h0= -2:0.04:1;
h=ones(size(x))*h0;Int=pchip(x,y);xx=linspace(x(1),x(end),500);
yy=y-h0;yyy=pchip(x,yy,xx);gt=yyy<0;
Area =-trapz(xx(gt), (yyy(gt)))
hold off
area(xx, min(ppval(Int,xx), h(1)), h(1), 'EdgeColor', 'none', 'FaceColor', 'b'),grid,
end

採用された回答

Alan Stevens
Alan Stevens 2020 年 8 月 23 日
Something like
...
k = 0;
for h0= -2:0.04:1;
k = k+1;
h=ones(size(x))*h0;Int=pchip(x,y);xx=linspace(x(1),x(end),500);
yy=y-h0;yyy=pchip(x,yy,xx);gt=yyy<0;
Area(k) =-trapz(xx(gt), (yyy(gt)));
...etc.
  3 件のコメント
Hamzah Mahmood
Hamzah Mahmood 2020 年 8 月 23 日
One more follow up question. Currently h0 is being stored as a single variable that is being overwritten, could I use a similar line of code to store that as the range I'm calculating for?
Alan Stevens
Alan Stevens 2020 年 8 月 23 日
You could rearrange the coding as follows:
h0= -2:0.04:1;
for k= 1:length(h0);
h=ones(size(x))*h0(k);Int=pchip(x,y);xx=linspace(x(1),x(end),500);
yy=y-h0(k);yyy=pchip(x,yy,xx);gt=yyy<0;
Area(k) =-trapz(xx(gt), (yyy(gt)));
... etc.
(notice that h0 now becomes h0(k) within the loop) then all values of h0 are available after the loop ends.

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

その他の回答 (0 件)

カテゴリ

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