필터 지우기
필터 지우기

how to extract the number of the ordered symbolic varible?

조회 수: 2 (최근 30일)
Steve Deltora
Steve Deltora 2022년 2월 1일
답변: Prateekshya 2023년 10월 20일
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
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.
Steve Deltora
Steve Deltora 2022년 2월 1일
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?

댓글을 달려면 로그인하십시오.

답변 (1개)

Prateekshya
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!

카테고리

Help CenterFile Exchange에서 Quadratic Unconstrained Binary Optimization (QUBO)에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by