In Problem 44260, multivariate polynomials were defined as a sum of monomial terms using|exponents|, a matrix of integers, and|coefficients|, a vector (follow the above link for an explanation). It can be useful to order the monomials. But first we need to define the total degree of a monomial as the sum of the exponents. For example, the total degree of 5*x is 1 and the total degree of x^3*y^5*z is 9.
Write a function
function [coeffs,exponents] = sortMonomials(coeffs,exponents)
to sort the monomials. Sort them first by descending total degree, and then for a given total degree, by lexicographical order of the exponents (by the first exponent, then the second, and so on, each in descending order). The coefficients should be sorted so they stay with the correct monomial.
Example: Consider the polynomial p(x,y,z) = 3*x - 2 + y^2 +4*z^2, which is represented as:
exponents = [1 0 0; 0 0 0; 0 2 0; 0 0 2], coefficients = [3; -2; 1; 4]
The sorted version is
exponents = [0 2 0; 0 0 2; 1 0 0; 0 0 0], coefficients = [1; 3; 1; 4].
You can assume that a given combination of exponents is never repeated.
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers9
Suggested Problems
-
Maximum running product for a string of numbers
2253 Solvers
-
Create a cell array out of a struct
2411 Solvers
-
Back to basics 6 - Column Vector
1102 Solvers
-
98 Solvers
-
Set some matrix elements to zero
621 Solvers
More from this Author9
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
The sorted coefficients in your example should be [1; 4; 3; -2].
The test suite is also comparing against an incorrect result in the last problem.