Say I have a vector like [10,3,4], is there a way to get, for instance, the second element directly? Something like:
[10,3,4](2)
Also, if I have a cell of anonymous functions,such as:
funcs = {@(s) s^2 , @(s) s+5},
is there a way to access them in a way like:
funcs{2}(s)
(Edit: funcs{2}(s) works. It turns out the error I got was because of something else!) What I want to do is to save the gradient of a function in a cell like $gradr$ and then be able to get its dot product with another vector. Something lik:
dot([gradr{1}(s),gradr{2}(s)],n)
I cannot assign each component a different name since I'll be using the numbering later.
Thank you in advance.
Edit: the answer to the first part of my question can be found here.
i) No, not really. You need to put [10, 3, 4]
into a variable a
and then get the second element a(2)
. (You can do it, but it's not worth it).
ii) Yes: just use funcs{2}(2)
, or feval(funcs{2}, 2)
.
iii) You can try something like:
>> inarg = 1;
>> cellfun(@(x)feval(x,inarg),funcs)
ans =
1 6