How to get vector output from solve?

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

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)
The "See also" at the bottom of the "solve" doc does not list syms or sym

Sign in to comment.

Answers (1)

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

Thanks, can you explain a bit what the above code does? and if its not obvious how to modify the above code to take in all solutions. I'm pretty new is there some other reference I should search for besides the documentation here? I'm trying to find the list of cycles of a directed graph for small instances just trying to get faster than trying every edge set I don't quite understand what your linked post has to do with finding binary solutions please explain also system is not independent far from it.
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.
I'm trying to get a list of all cycles not just a basis. Can you explain how your previous code X = cell(size(x)); [X{:}] = solve(I*x == zeros(n,1) , x) works and how to modify it to take in all solutions?
It already takes in all solutions.
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 Is there a different solver I should be using? I don't see how to get all zero one solutions by this pseudoinverse method.
"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" ?
I is the directed incidence matrix sorry about the confusion.
So directed cycles are solutions to I*x=0 x \in {0,1 } how do I solve that?
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.
Sorry I wrote I*x=1 instead of I*x=0 x still confined to be 0,1 I don't know what you mean by solutions to I^m indexed at (J,K) ~= 0 I is not square so I^m does not make sense.
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.

Sign in to comment.

Asked:

on 27 Aug 2017

Commented:

on 30 Aug 2017

Community Treasure Hunt

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

Start Hunting!