How to get vector output from solve?
Show older comments
How to get vector output from solve? When I do something like y=solve( I*x == zeros(n,1) , x ); (n moderately large) I can get values y.x1 etc but I want to get the entire vector of x. Also if anyone knows the best way to get binary solutions I'm currently involving x.*x =x in the set of equations but this seems potentially inefficient.
p.s to any mathworks staff I think the documentation for solve could really be improved with references to syms and sym as well as explicitly stating that you can do two sets of equations by putting a comma in between
2 Comments
Karan Gill
on 29 Aug 2017
Thanks for the doc feedback.
What problem did you run into with syms and sym? By reference, do you mean a link? syms or sym can be found via a Google search or documentation search.
Why do you prefer entering equations with commas instead of using an array?
Karan (Symbolic doc)
Walter Roberson
on 30 Aug 2017
The "See also" at the bottom of the "solve" doc does not list syms or sym
Answers (1)
Walter Roberson
on 27 Aug 2017
X = cell(size(x));
[X{:}] = solve(I*x == zeros(n,1) , x)
"as well as explicitly stating that you can do two sets of equations by putting a comma in between"
Historically that was documented up to R2012a, in which release using a vector or array of equations was not documented or was not permitted. The documentation since then has not been clear as to whether multiple equations are permitted outside of a vector or array of equations; I have formed the impression that they intend to discourage lists of equations.
"Also if anyone knows the best way to get binary solutions"
I recommend not using solve() for this purpose unless you need to work with symbolic values. If your arrays can be reduced to numeric values, I recommend looking at https://www.mathworks.com/matlabcentral/answers/16192-inversion-of-a-boolean-matrix
For symbolics, you could experiment with using
assume(in(x,'integer') & x>=0 & x<=1)
however, in general the inverse would not be binary.
For more robust symbolics, you could wander into MuPAD Galois Fields. You will need to use evalin(symengine) or feval(symengine) in order to create these objects.
10 Comments
Standardtrickyness
on 28 Aug 2017
Edited: Standardtrickyness
on 28 Aug 2017
Walter Roberson
on 28 Aug 2017
https://www.mathworks.com/matlabcentral/fileexchange/4266-grtheory-graph-theory-toolbox has a routine to find all independent cycles for a graph.
You appeared to be doing solve() with a matrix multiplication. A theoretic way of solving that is to multiply both sides by a matrix inverse, as that gets you the identity matrix on one side and the solutions on the other. Derek's functions in the Question I linked to gives an efficient calculation of the inverse of a binary matrix.
Standardtrickyness
on 28 Aug 2017
Walter Roberson
on 28 Aug 2017
It already takes in all solutions.
Standardtrickyness
on 28 Aug 2017
Walter Roberson
on 28 Aug 2017
"So I've found that your code requires specifying the size of X first which doesn't work if you want X to be the matrix of all solutions"
MATLAB's symbolic toolbox does not permit a single symbolic variable name to stand in for a matrix. For something like
I*x == zeros(n,1)
then presuming that I = eye(n), you need x = sym('x', [1, n]) so that you are creating a vector of equations on the left hand side. Each X{K} on the left hand side of the solve will correspond to the list of solutions for x(k)
At the moment, I do not understand what your solve() have to do with finding "the list of cycles of a directed graph for small instances" ?
Standardtrickyness
on 29 Aug 2017
Edited: Standardtrickyness
on 29 Aug 2017
Walter Roberson
on 29 Aug 2017
Edited: Walter Roberson
on 29 Aug 2017
Could you link to a graph theory page demonstrating that logic? In my experience, directed cycles would be solutions to I^m indexed at (J,K) ~= 0, for some integer m equal to the length of the cycle.
Standardtrickyness
on 29 Aug 2017
Walter Roberson
on 29 Aug 2017
Do not use incidence matrices for this purposes: use a square adjacency matrix.
If you have an adjacency matrix, A, then the places where A^1 is non-zero tell you all the places that can be reached from somewhere after 1 step, and the places where A^2 is non-zero tell you all the places that can be reached from somewhere after 2 steps, and so on.
If you take B = A^n for some integer n, and B(J,J) is non-zero, then if you started at node J at the beginning and you took n steps, then you were able to reach node J after n steps -- indicating that node J is part of a cycle that is n steps long. The cycle might have been repeated several times, so possibly the smallest cycle from J was any of the prime factors of n long.
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!