accessing single function from an array of functions

I have defined a matrix of functions:
a=@(t) (1+0.8*sin(2*pi*t));
b=@(t) .8*(1-0.8*sin(2*pi*t));
z = @(t) 0*t;
%T = matlabFunction(fmat);
T=@(t) [-3*a(t) a(t) a(t) a(t) z(t) z(t);...
a(t) -5*a(t) a(t) a(t) a(t) a(t); ...
a(t) a(t) -5*a(t) a(t) a(t) a(t); ...
b(t) b(t) b(t) -5*b(t) b(t) b(t); ...
b(t) b(t) b(t) b(t) -5*b(t) b(t); ...
a(t) a(t) a(t) b(t) 5*b(t) -3*a(t)-6*b(t)];
If I want to access just a single function from the matrix, how do I do it? I have tried several approaches and keep getting: Error: Invalid array indexing.

1 件のコメント

Walter Roberson
Walter Roberson 2022 年 3 月 4 日
See https://www.mathworks.com/matlabcentral/answers/539642-is-it-possible-to-have-an-array-of-function-handles#comment_2020739

サインインしてコメントする。

 採用された回答

Torsten
Torsten 2022 年 3 月 4 日
編集済み: Torsten 2022 年 3 月 4 日

0 投票

f = {@(t) t, @(t) exp(t), @exp} % the second and third are equivalent
SecondFunction = f{2};
ValueOfSecondFunction = SecondFunction(1)
Does that help ?

6 件のコメント

Barbara Margolius
Barbara Margolius 2022 年 3 月 4 日
I have also tried defining the function in this way:
Tcell = {@(t) -3*a(t), a, a, a, z, z;...
a, @(t) -5*a(t), a, a, a, a; ...
a, a, @(t) -5*a(t), a, a, a; ...
b, b, b, @(t) -5*b(t), b, b; ...
b, b, b, b, @(t) -5*b(t), b; ...
a, a, a, b, @(t) 5*b(t), @(t)-3*a(t)-6*b(t)};
The trouble is that I want to exponentiate the matrix for fixed values of t and define a new function in terms of that. Something like
myLT = @(s)eye(mysize);
% temporary definition***
C=eye(mysize);
for k=0:n-1
tobeexp = T(k*deltat)*deltat;
myLT = @(s) myLT(s)*expm(tobeexp-s*C);
end
This does work, but I can't figure out how to get to a particular function within the matrix of functions.
Torsten
Torsten 2022 年 3 月 4 日
編集済み: Torsten 2022 年 3 月 4 日
myLT = @(s)eye(mysize);
Why does eye(mysize) depend on a parameter s ?
This does work, but I can't figure out how to get to a particular function within the matrix of functions.
Within which matrix of functions ?
The trouble is that I want to exponentiate the matrix for fixed values of t and define a new function in terms of that.
Why trouble ?
a=@(t) (1+0.8*sin(2*pi*t));
b=@(t) .8*(1-0.8*sin(2*pi*t));
z = @(t) 0*t;
Tcell = @(t){ -3*a(t), a(t), a(t), a(t), z(t), z(t);...
a(t), -5*a(t), a(t), a(t), a(t), a(t); ...
a(t), a(t), -5*a(t), a(t), a(t), a(t); ...
b(t), b(t), b(t), -5*b(t), b(t), b(t); ...
b(t), b(t), b(t), b(t), -5*b(t), b(t); ...
a(t), a(t), a(t), b(t), 5*b(t), -3*a(t)-6*b(t)};
Tcell_45_at_t_eq_2 = Tcell(2)(4,5);
Please include a piece of code and explain comprehensively what you are trying to do.
Barbara Margolius
Barbara Margolius 2022 年 3 月 4 日
Sorry, it is a code snippet. mysize is just 6. It does not depend on s. It is so the loop definition of the function will work.
Barbara Margolius
Barbara Margolius 2022 年 3 月 4 日
a = @(t)(1+0.8*sin(2*pi*t));
b = @(t)0.8*(1-0.8*sin(2*pi*t));
z = @(t)0*t;
n = 10;
%T = matlabFunction(fmat);
% T is a function of t which returns a 6-by-6 matrix.
% Each element of T is derived from the outputs of functions a, b, and z,
% which are also functions of t.
T = @(t)[ ...
-3*a(t) a(t) a(t) a(t) z(t) z(t);...
a(t) -5*a(t) a(t) a(t) a(t) a(t); ...
a(t) a(t) -5*a(t) a(t) a(t) a(t); ...
b(t) b(t) b(t) -5*b(t) b(t) b(t); ...
b(t) b(t) b(t) b(t) -5*b(t) b(t); ...
a(t) a(t) a(t) b(t) 5*b(t) -3*a(t)-6*b(t); ...
];
mysize = max(size(T(0)));
myLT = @(s)eye(mysize);
C=diag([1 2 3 -4 -3 0]);
deltat = 1/n;
mysize = max(size(T(0)));
myLT = @(s)eye(mysize);
C=diag([1 2 3 -4 -3 0]);
for k=0:n-1
tobeexp = T(k*deltat)*deltat;
myLT = @(s) myLT(s)*expm(tobeexp-s*C);
end
myLT = @(s)det(eye(mysize)-myLT(s));
myLT(.4)
28.0296
h=0:.01:.99;
plot(myLT(h));
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix
matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Torsten
Torsten 2022 年 3 月 4 日
編集済み: Torsten 2022 年 3 月 4 日
h=0:.01:.99;
myLTh = arrayfun(myLT,h);
plot(h,myLTh)
But to be honest: I can't decipher what you are doing in your code.
Barbara Margolius
Barbara Margolius 2022 年 3 月 4 日
thanks!

サインインしてコメントする。

その他の回答 (1 件)

Voss
Voss 2022 年 3 月 4 日

0 投票

a = @(t)(1+0.8*sin(2*pi*t));
b = @(t)0.8*(1-0.8*sin(2*pi*t));
z = @(t)0*t;
%T = matlabFunction(fmat);
% T is a function of t which returns a 6-by-6 matrix.
% Each element of T is derived from the outputs of functions a, b, and z,
% which are also functions of t.
T = @(t)[ ...
-3*a(t) a(t) a(t) a(t) z(t) z(t);...
a(t) -5*a(t) a(t) a(t) a(t) a(t); ...
a(t) a(t) -5*a(t) a(t) a(t) a(t); ...
b(t) b(t) b(t) -5*b(t) b(t) b(t); ...
b(t) b(t) b(t) b(t) -5*b(t) b(t); ...
a(t) a(t) a(t) b(t) 5*b(t) -3*a(t)-6*b(t); ...
];
% the 6-by-6 matrix returned from the function T when it is called with
% input 1:
result = T(1)
% get some element of the result (the result is the matrix returned from
% calling the function T with input 1):
result(4,5)
% trying to do the same with the output of the function T called with
% input 1 directly doesn't work in MATLAB (the output from T should be
% stored in a variable first, i.e., 'result' here):
try
T(1)(4,5);
catch ME
disp(ME.message);
% but you can index T(1) without storing in a variable, if you really
% want to:
subsref(T(1),substruct('()',{4,5}))
end

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by