Array indices: using function handles and fminsearch
Show older comments
I have the following code:
P = randn(50);
for i = 1:length(P)
vec{i} = @(alpha)exp(-(i-1)*alpha);
end
G = @(p)p(1)*diag(vec(p(2)));
fun = @(p)norm(eye(50) - G(p)*P);
p0 = [0 0];
[p, fval] = fminsearch(fun, p0)
The idea is essentially to minimize the 2-norm of the matrix
. On executing this, I get the following error:
Array indices must be positive integers or logical values.
Error in @(p)p(1)*diag(vec(p(2)))
Error in @(p)norm(eye(50)-G(p)*P)
Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});
Since the dimension of the matrix G is quite large, it is not possible for me to hard-code the diagonal entries in the diag( ) function, which is why I'm using the cell array of function handles.
I have tried replacing the variable alpha with p(2) directly, and a couple of other variations on this. All of these give me the same error. I have a feeling that the error is elementary, but since I'm new to using function handles, I am unable to rectify this. I have seen questions on similar lines, but am not able to find a way to adapt their solutions to mine.
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing 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!