Clear Filters
Clear Filters

how to extract the number of the ordered symbolic varible?

2 views (last 30 days)
A = "E = 6*q1*q2 + q1*q3 - 2*q1*q4 - 2*q2*q3 - 12*q1*q5 - 8*q2*q4 - 12*q2*q5 + 4*q3";
I was coding about the QUBO which is a math equation like A.
the next step of QUBO is to put the variable coefficients into matrix.
the rule is, for example 6*q1*q2, the number of first variable is the row and the number of seceond variable is the column, and that is put 6 into the row1 column2.
and if there is only one variable, it's will seem as the row and column for the same number, i.g. q3 put at the row3 column3.
the result of A format into QUBO will be
Q = [
0 6 1 -2 -12;
0 0 -2 -8 -12;
0 0 4 0 0;
0 0 0 0 0;
0 0 0 0 0
]
the problem is that I have no idea what function can do this procedure.
thanks.
  2 Comments
Sena Koçak
Sena Koçak on 1 Feb 2022
You can use the coeffs(fun, var) command. If you define the variable for your function and assign them into the matrix, you can easily obtain a matrix like QUBO.
Steve Deltora
Steve Deltora on 1 Feb 2022
thanks for answer, but one more question.
i use
[c, t] = coeffs(a, ["q1"])
but the result is very weird.
is coefficient is 6*q1+q3-2*q4-12*q5
what should i do next?

Sign in to comment.

Answers (1)

Prateekshya
Prateekshya on 20 Oct 2023
Hi Steve,
As per my understanding, you are trying to extract the coefficient of the terms which have more than one variables. As Sena suggested in the comments, "coeffs" function can be used to achieve the desired result. Consider the following code:
[c_q1,text] = coeffs(6*q1*q2 + q1*q3 - 2*q1*q4 - 2*q2*q3 - 12*q1*q5 - 8*q2*q4 - 12*q2*q5 + 4*q3,q1)
Here we are trying to find out the coefficients of "q1" in the given expression. After executing the above line "c_q1" contains a vector of two outputs. "c_q1(1)" contains the terms which have "q1" and "c_q1(2) contains the terms which do not have "q1". When we print "c_q1" we get the following result:
c_q1 =
[6*q2 + q3 - 2*q4 - 12*q5, 4*q3 - 2*q2*q3 - 8*q2*q4 - 12*q2*q5]
Now the expression at "c_q1(1)" can again be passed through "coeffs" function with a different variable to get the desired coefficients. Which means:
[c_q2,text] = coeffs(c_q1(1),q2);
Here "c_q2(1)" will store "6". Now this value can be stored at "Q(1,2)". I hope this helps!

Categories

Find more on Quadratic Unconstrained Binary Optimization (QUBO) in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!