0

Im tring to substract the diagonal values with eigval and store the new value in the matrix Diagonal:

   CovarianceMatrix=[8 -3 1;2 1 0;3 4 5];
   Col=3;
   Row=3;
   store=1;
    syms eigval;

   for loop1= Col:-1:1
    Rw=1;
    syms eigval;
    for loop2= 1:Row
        if Rw==loop1
            Diagonal= (CovarianceMatrix(Rw,loop1)-eigval);
             Fix_Diagonal_2(loop2,store)=sym(Diagonal);
        else
            Diagonal= CovarianceMatrix(Rw,loop1);
            Fix_Diagonal_2(loop2,store)=Diagonal;
        end

        Rw=Rw+1;
        loop1=loop1-1;

        if loop1==0
             loop1=3;
        end

    end
    store=store+1;

end

But because I'm using a symbolic variable it gives an error:

      The following error occurred converting from sym to double:
      Error using mupadmex
      Error in MuPAD command: DOUBLE cannot convert the input expression into a double
      array.

      If the input expression contains a symbolic variable, use the VPA function      
      instead.

How can I solve this? I would like to copy the new substracted value into the diagonal matrix.

user3303896
  • 57
  • 1
  • 2
  • 6
  • 1
    Please use proper code indentation, it's very difficult to read code without. – Daniel Feb 16 '14 at 23:28
  • 2
    What is symbolic? You're code isn't runnable. Provide some simple input values that replicate your issue. And post the *entire error message* including line numbers – which line in your code throws the error? – horchler Feb 16 '14 at 23:40

1 Answers1

4

Here's a simple bit of code that exhibits your same error so maybe it will help clarify the issue:

syms x;       % Create symbolic variable
a1 = rand(2); % Floating point array 1
a2 = rand(2); % Floating point array 2
d = a1(1)-x;  % This is now a symbolic expression
a2(1) = d;    % Error: you can't store a symbolic expression in a double array

which in R2013b (and R2015b) returns

The following error occurred converting from sym to double:
Error using mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double
array.
If the input expression contains a symbolic variable, use the VPA function
instead.

vpa can't be used here because x is a symbolic variable that hasn't been defined rather than a symbolic value (vpa(d) has an inconsequential effect).

For your code, the error likely occurs on this line:

Fix_Diagonal_2(loop2,store)=Diagonal;

You can't use vpa because eigval is a symbolic variable with no value. You can possibly solve your problem by casting Fix_Diagonal_2 to sym:

Fix_Diagonal_2 = sym(Fix_Diagonal_2);

You'll probably want to do that outside of the for loops. I also don't see why you're redefining eigval on every iteration of the outer loop.

horchler
  • 18,384
  • 4
  • 37
  • 73
  • I already tried as what u explain but it still error!! Now, I already put more clear my program. That is a part of my program to calculate the eigen value from the matrix. and that code using to keep the second diagonal (det=1st diag- 2nd diag) and then multiply and sum all of the value. So it will be use to be substracted with First Diag Value to get the eigen Value. Can u please help me to solve that.. – user3303896 Feb 17 '14 at 00:11
  • @user3303896: Please reread the second part of my answer. `Diagonal` is already symbolic so there's no point in trying to recast it. It's `Fix_Diagonal_2 = sym(Fix_Diagonal_2);`. Your code still isn't runnable as you haven't specified `Fix_Diagonal_2`. Maybe you just need to preallocate it as zeros: something like `Fix_Diagonal_2 = sym(zeros(size(CovarianceMatrix)));`? And you're still redefining `eigval` on every iteration of the outer loop. – horchler Feb 17 '14 at 15:00