Philosophical question: How to get a vector from the output of solve? Why isn't it a vector already?
Mostra commenti meno recenti
I am solving an equation of several variables, and the result is a struct.
SNvec = solve(nvecEqabcd,[a1,b1,c1,d1]);
I want to write it to my latex file. So I sprintf a string that will eventually get printed to my latex file (after some processing to make it pretty):
string.steadyn=sprintf('The steady state of the nvec equation is: $rupup = %f, rdowndown = %f, rupdown = rdownup = %f, r00 = %f, rbb = %f.$',struct2array(SNvec),1-sum(struct2array(SNvec)))
But today, for some reason, struct2array doesn't work. I look that up and apparently it works for some versions of matlab but it isn't really meant to be used. So I could change my sprintf to use the struct variables, SNvec.a1, etc or I could set the output of 'solve' to four different variables, then assign them to a vector:
[Eqa1, Eqb1, Eqc1, Eqd1] = solve(nvecEqabcd,[a1,b1,c1,d1]);
Equilnvec = [Eqa1, Eqb1, Eqc1, Eqd1] ;
But in my mind, the solution to my equation is a single object, a vector, with whatever dimension it happens to have, it might be twenty dimensions next time. The fact that matlab, which was invented to deal with matrices and vectors, doesn't naturally treat this thing like a vector, suggests to me that I'm thinking about something the wrong way. Why do I have to manually turn it into a vector? Is there a smart way to turn the struct into a vector? Should I be doing something else?
Obviously, low priority here, I can get my code to run and am just curious if anybody has insight about how to think about this.
Risposta accettata
Più risposte (1)
John D'Errico
il 20 Mar 2023
Modificato: John D'Errico
il 20 Mar 2023
I tried it with a vector, wondering if that might make solve work more easily. And it does work.
A = magic(3);
X = sym('X',[3,1]);
B = [1;2;3];
Xsol = solve(A*X == B)
But struct2array has no problem.
struct2array(Xsol)
syms a b c
abc = solve(A*[a;b;c] == B)
struct2array(abc)
Which also seems to work. So as long as a unique solution is returned, it works nicely. The vector was not necessary. The problem for you was surely that solve is returning multiple solutions.
In the case where there are multiple solutions, maybe you need to use struct2cell.
syms x y
xy = solve(x^2 + y^2==2,x+y==1)
xycell = struct2cell(xy)
And now with some more manipulations, convert those results into an array as you might desire. Perhaps like this:
horzcat(xycell{:})
Hope this helps.
3 Commenti
syms x1 x x10 X
Y = [x1; x; x10; X]
A = randi(9,4,4)
B = [1;2;5;7];
Xsol = solve(A*Y == B)
That is, the order of fields in the struct might not be what you are expecting, so it is safest to extract particular variables or to use subs
subs(Y, Xsol)
John D'Errico
il 20 Mar 2023
Yes. You would need to be careful about extracting results into an array, as you might trip over ambiguities since the elements of an array have no names.
Emily
il 20 Mar 2023
Categorie
Scopri di più su Operators and Elementary Operations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
