title: Force entries of a matrix to be a variable
body: <p>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</p>
  
  <pre><code>X=[1,2,3;4,5,6;7,8,9];
  </code></pre>
  
  <p>I would like make this</p>
  
  <pre><code>p(1)=a1;
  p(2)=a2;
  p(3)=a3;
  p(4)=a4;
  X=[1, 2, a1 ; a2, 5, a3 ; 7, 8, a4];
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>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? </p>
  
  <p>EDIT:</p>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>