Question

I have a square matrix that I need to use with fminsearch. Some of the values of the matrix need to be variable because they are the values that I will be using with fminsearch, and I need to preserve their location in the matrix. So for example, with

X=[1,2,3;4,5,6;7,8,9];

I would like make this

p(1)=a1;
p(2)=a2;
p(3)=a3;
p(4)=a4;
X=[1, 2, a1 ; a2, 5, a3 ; 7, 8, a4];

So that I could do operations on X to create something to be minimized with fminsearch. For example, suppose I wanted to find a1, a2, a3, and a4 so that I minimized C in the following code, which computes the summed entropy of a given matrix:

Ent=zeros(size(X,1),1);
    for k=1:size(X,2);
        const=X(k,:);
        logX=log(X(k,:));
        logX=logX';
        Ent(k,:)=const*logX;
    end
Ent=-Ent;
C=sum(Ent);

Is this possible with MATLAB? Suppose further I had an nxn matrix with q paramaters, how would I adjust the idea to minimize the same C?

EDIT:

I figured out how to do what I want. However it has come to me that a gradient descent algorithm is really a better way of doing this. I'll post a sample of what I was doing:

function test()
[new_param entropy]=fminsearch(@Cost,[3,3,3]);

function C=Cost(p)
X=rand(5);
X(1,1)=p(1);
X(2,2)=p(2);
X(3,3)=p(3);
Ent=zeros(size(X,1),1);
for k=1:size(X,2);
    const=X(k,:);
    logX=log(X(k,:));
    logX=logX';
    Ent(k,:)=const*logX;
end
Ent=-Ent;
C=sum(Ent);
end
X(1,1)=new_param(1);
X(2,2)=new_param(2);
X(3,3)=new_param(3);
X
new_param
entropy
end
Was it helpful?

Solution

I think that you could try something like this:

A = @(x) [1 2 x(1); 3 4 x(2); 5 6 x(3)]; 

or, alternatively, making use of the Symbolic Toolbox:

syms x y z

A =  [1 2 x; 3 4 y; 5 6 z];

OTHER TIPS

I may misunderstand, but I think it is as simple as this:

X=[1, 2, p(1) ; p(2), 5, p(3) ; 7, 8, p(4)];

After updating p just run this statement again and you have your desired X.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top