Constructing a matrix of function handles

49 vues (au cours des 30 derniers jours)
varmidh
varmidh le 23 Sep 2016
Commenté : Steven Lord le 23 Sep 2016
Is there a way to recursively construct a matrix of function handles? It is possible to construct a matrix of function handles as follows
GS=@ (x)[states{1}(x)'*states{1}(x) states{1}(x)'*states{2}(x);
states{2}(x)'*states{1}(x) states{2}(x)'*states{2}(x)],
where states{i}(x) are function handles themselves. However, as I would like to construct this matrix for arbitrary n (say n=100) I would like to create the gram matrix in a for loop. I tried to create my own function that takes as input the n vectors and constructs the Gram matrix of their overlaps, i.e. G(i,j)=@ (x) states{i}(x)'*states{j}(x).
This clearly does not work since one cannot then pass the argument into G; the issue being that matlab requires a pair of indices in the command G() and not the argument of the function handle. However it is clearly possible to create a matrix of function handles as shown above, and yet not possible to create it recursively. Does anyone know of a workaround for this.
  2 commentaires
Star Strider
Star Strider le 23 Sep 2016
‘Here states{i}(x) are function handles themselves. ’
It would help if we have all the relevant parts of your code, and specifically what ‘states’ actually are. Those don’t look like valid function calls to me.
You can certainly create a matrix of function calls. Anonymous functions have some restrictions on what you can do with them.
Steven Lord
Steven Lord le 23 Sep 2016
They are valid function calls. The states variable is a cell array each cell of which contains a function handle. First you index into the cell array to retrieve a function handle, then you evaluate that function handle.
states = {@sin, @cos, @tan};
% Two step approach
cosineHandle = states{2}
cosineHandle(pi)
% One step approach
states{2}(pi)
% Alternate one step approach
cos(pi)

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 23 Sep 2016
You cannot create a nonscalar array of function handles.
You can create a function handle that returns a nonscalar array, but that's different.
You can create a cell array each cell of which contains a function handle, but that's also different.
For the application you described, having a function handle that returns a nonscalar array is probably going to be the easiest.
f = @(x) x(:); % The output is of size [numel(x) 1]
GS = @(x) f(x)*f(x).'; % The output is of size [numel(x) numel(x)]
x = 1:10;
multiplicationTable = GS(x)
  2 commentaires
varmidh
varmidh le 23 Sep 2016
Hi Steven.
First thank you for the quick response. That's not exactly what I am looking for. But this might be due to the fact that I was not explicit enough with the minimal working example.
Ultimately I want to minimize a function of the GS matrix over all possible vectors x. For each choice of x I have a procedure that computes the orbit of x under the action of a set of matrices (these is what states{i}(x) are). Then GS is a matrix that contains the pairwise overlaps of all vectors in this orbit. That is for any two vectors in the set the overlap is a single number that needs to be placed in the appropriate slot in GS.
Your proposed solution simply constructs the outer product between these two vectors, not the overlap. I tried to modify your proposed solution but my main problem persists. There is no way of doing this without having to invoke two for loops of function handles.
Steven Lord
Steven Lord le 23 Sep 2016
Can you show a specific example where you construct the states cell array of function handles and compute GS for a specific vector? Your example doesn't need to use n = 100; something like n = 3 or n = 4 should serve to explain more clearly your ultimate goal.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Function Creation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by