Can I store a function including multiple variable?
Ältere Kommentare anzeigen
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 Kommentare
Prudhvi Peddagoni
am 22 Okt. 2020
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
am 22 Okt. 2020
chia ching lin
am 24 Okt. 2020
Antworten (2)
Steven Lord
am 22 Okt. 2020
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)
Walter Roberson
am 22 Okt. 2020
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 Kommentare
chia ching lin
am 24 Okt. 2020
Walter Roberson
am 24 Okt. 2020
cellfun(@(F) F(x, t), state)
Kategorien
Mehr zu Transmitters and Receivers finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!