accessing single function from an array of functions
조회 수: 6 (최근 30일)
이전 댓글 표시
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
2022년 3월 4일
See https://www.mathworks.com/matlabcentral/answers/539642-is-it-possible-to-have-an-array-of-function-handles#comment_2020739
채택된 답변
Torsten
2022년 3월 4일
편집: Torsten
2022년 3월 4일
f = {@(t) t, @(t) exp(t), @exp} % the second and third are equivalent
SecondFunction = f{2};
ValueOfSecondFunction = SecondFunction(1)
Does that help ?
추가 답변 (1개)
Voss
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;
%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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!