how to extract the number of the ordered symbolic varible?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Sena Koçak
2022-2-1
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.
回答(1 个)
Prateekshya
2023-10-20
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Quadratic Unconstrained Binary Optimization (QUBO) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!