# Is there a one line implementation for this?

1 view (last 30 days)
Fatwir Sheikh Mohammed on 23 Nov 2022
Commented: Walter Roberson on 23 Nov 2022
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
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (2)

Voss on 23 Nov 2022
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
result = 2×2 cell array
{[2.3198 2.4826 1.1516 0.4692 0.3833 0.7562 1.9290 2.6895 1.5100 0.5804]} {[ 1.7165 0.6596 0.3716 0.5201 1.3280 2.6121 2.1253 0.8646 0.4021 0.4321]} {[4.7465 0.1125 0.8671 3.1830 0.0340 0.7475 2.3904 0.0011 0.6362 1.9124]} {[2.7183 7.3891 20.0855 54.5982 148.4132 403.4288 1.0966e+03 2.9810e+03 8.1031e+03 2.2026e+04]}
one_line_result = reshape(mat2cell(double(subs(exp([A{:}]),t,{1:10})),1,10*ones(1,4)),2,[])
one_line_result = 2×2 cell array
{[2.3198 2.4826 1.1516 0.4692 0.3833 0.7562 1.9290 2.6895 1.5100 0.5804]} {[ 1.7165 0.6596 0.3716 0.5201 1.3280 2.6121 2.1253 0.8646 0.4021 0.4321]} {[4.7465 0.1125 0.8671 3.1830 0.0340 0.7475 2.3904 0.0011 0.6362 1.9124]} {[2.7183 7.3891 20.0855 54.5982 148.4132 403.4288 1.0966e+03 2.9810e+03 8.1031e+03 2.2026e+04]}
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Walter Roberson on 23 Nov 2022
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)
result = 2×2 cell array
{[ 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121 -0.5440]} {[0.5403 -0.4161 -0.9900 -0.6536 0.2837 0.9602 0.7539 -0.1455 -0.9111 -0.8391]} {[1.5574 -2.1850 -0.1425 1.1578 -3.3805 -0.2910 0.8714 -6.7997 -0.4523 0.6484]} {[ 1 2 3 4 5 6 7 8 9 10]}
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 CommentsShowHide 1 older comment
Walter Roberson on 23 Nov 2022
Ah you are right, I mised the exp()

Sign in to comment.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by