Can I store a function including multiple variable?
古いコメントを表示
Hi,
Im new to matlab, is it able to store function including multiple variable which still can be used? Something like this
x=linspace(1,10,100);
t=linspace(1,5,50);
f=@(x,t,theta) sin(x+t(a)+theta)
for i=1:10
state{i}=f(:,:,i);
end
where i expect that (but it is not)
state={sin(x+t(a)+1) sin(x+t(a)+2) sin(x+t(a)+3) ... sin(x+t(a)+10)}
so i can used a for loop on t to make a annimation like this
for a=1:length(t)
for b=1:length(state)
plot(x,state{b}(a));
end
end
Is it possible to do something like above or Im using the wrong function?
3 件のコメント
Prudhvi Peddagoni
2020 年 10 月 22 日
can you elaborate more on what you are trying to do?
what is the variable a in the anonymous function?
what should the variable state contain and what should be it's length?
chia ching lin
2020 年 10 月 22 日
chia ching lin
2020 年 10 月 24 日
回答 (2 件)
Steven Lord
2020 年 10 月 22 日
You can't call a function handle using :. If you want the anonymous function to "remember" and use the values of x and t that you've defined in the workspace, you can do that.
x = 1:10;
y = 2;
f = @(theta) y*sin(x+theta);
z1 = f(7)
% check
z2 = 2*sin(8:17)
1 件のコメント
chia ching lin
2020 年 10 月 22 日
Walter Roberson
2020 年 10 月 22 日
for i=1:10
state{i}=@(x,t)f(x,t,i);
end
Note that this will be a cell array of function handles and if you wanted an array of results you would need to cellfun
cellfun(@(F) F(x, y), state)
2 件のコメント
chia ching lin
2020 年 10 月 24 日
Walter Roberson
2020 年 10 月 24 日
cellfun(@(F) F(x, t), state)
カテゴリ
ヘルプ センター および File Exchange で Transmitters and Receivers についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!