フィルターのクリア

store the output of a for loop in an array

1 回表示 (過去 30 日間)
Yassine
Yassine 2012 年 5 月 15 日
Hi everyone... I want to store the output of 'q' in an array from the following code:
F= @(x) (cos(x))./sqrt(1+sin(x));
for n=0:0.1:pi/2
q = quad(F,0,n)
end
I tried to use q(n) instead of q but it gives me an error. Thanks for your time and help!

回答 (3 件)

Oleg Komarov
Oleg Komarov 2012 年 5 月 15 日
I did not preallocate (up to you)
F = @(x) (cos(x))./sqrt(1+sin(x));
n = 0:0.1:pi/2;
for ii = 1:numel(n)
q(ii) = quad(F,0,n(ii))
end
You were not able to use q(n) directly because n in that case should indicate which position in q the result should be allocated to. There's no such a thing as position 0.1 and the counting reference starts from 1, thus no position 0 either.
Formally stating, an array can be indexed with positive integer values: 1,2,... etc

Thomas
Thomas 2012 年 5 月 15 日
try
F= @(x) (cos(x))./sqrt(1+sin(x));
count=1;
for n=0:0.1:pi/2
q (count)= quad(F,0,n);
count=count+1;
end
q

Andrei Bobrov
Andrei Bobrov 2012 年 5 月 15 日
q = arrayfun(@(n)quad(@(x)cos(x)./sqrt(1+sin(x)),0,n),0:0.1:pi/2);

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by