Is there a one line implementation for this?
Afficher commentaires plus anciens
t = sym('t',[1,10]);
A = {};
A {1,1} = sin(t);
A {1,2} = cos(t);
A {2,1} = tan(t);
A {2,2} = t;
result = {};
% For the below lines, it would be nice to have a one line implementation!
for i = 1:2
for j = 1:2
result{i,j} = eval(subs(exp(A{i,j}),t,{1:10}));
end
end
Réponses (2)
t = sym('t',[1,10]);
A = {};
A {1,1} = sin(t);
A {1,2} = cos(t);
A {2,1} = tan(t);
A {2,2} = t;
result = {};
% For the below lines, it would be nice to have a one line implementation!
for i = 1:2
for j = 1:2
result{i,j} = eval(subs(exp(A{i,j}),t,{1:10}));
end
end
result
one_line_result = reshape(mat2cell(double(subs(exp([A{:}]),t,{1:10})),1,10*ones(1,4)),2,[])
syms t; M = [sin(t), cos(t); tan(t), t]; result = arrayfun(@(I,J) double(subs(M(I,J), t, 1:10)), [1 1; 2 2], [1 2; 1 2], 'uniform', 0)
Note: you should never eval() a symbolic expression or symbolic function. eval() has no documented meaning for symbolic expressions or symbolic functions, and the undocumented behaviour will give you errors or unexpected results.
2 commentaires
syms t;
M = [sin(t), cos(t); tan(t), t];
result = arrayfun(@(I,J) double(subs(exp(M(I,J)), t, 1:10)), [1 1; 2 2], [1 2; 1 2], 'uniform', 0)
Walter Roberson
le 23 Nov 2022
Ah you are right, I mised the exp()
Catégories
En savoir plus sur Symbolic Math Toolbox dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!