How to select element of matrix that is created with function handle ?
Show older comments
Let say we have matrix defined as
A = @(a,n) [a 1 2+1i n*a; 0 1 3+n*1i a; a^2 0 1 a; a 0 0 a];
M1 = @(a)eye(4,4);
for n = 1:10
M1 =@(a) M1(a)*A(a,n); % ?
end
How to select M1(1,4) that is dependent on a?
m14 = @(a)M1(a)(1,4) %doesnt work
Accepted Answer
More Answers (1)
Guillaume
on 16 Oct 2015
First of all, the code you've written does not define any matrix. It defines functions that generate matrices, but until you call the functions, no matrix exist.
Secondly, I'm going to assume that the M1 recursion is intended and that you intend the final M1 to be:
M1 = @(a) eye(4,4)*A(a,1)*A(a,2)*A(a,3)*...*A(a,10)
I'm not sure of the performance impact of the recursion, the workspace of the final anonymous function is going to be huge. You may be better off going with a non-anonymous function that contains a loop.
Anyway, to answer your question, matrix indexing is translated into calls to subsref, so just use that:
m14 = @(a) subsref(M1(a), struct('type', '()', 'subs', {{1 4}}))
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!