Assign Values to Each Element of Symbolic Array
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Kevin Bachovchin
am 12 Mai 2014
Kommentiert: Deepak Ramaswamy
am 14 Mai 2014
Hello,
I have an array of symbolic values and I want to assign values to each symbolic variable in the array?
For example if I have
syms x1 x2 x3 x4 x5
X = [x1 ; x2 ; x3 ; x4 ; x5];
Values = [1 ; 2 ; 3 ; 4 ; 5];
I want to assign each symbolic element of X equal to the corresponding number in Values (in this case x1 = 1, x2 = 2, etc). Is there a way to do this?
X = deal(values) does NOT work because it assigns X = [1 ; 2 ; 3 ; 4 ; 5] rather than x1 = 1, x2 = 2, x3 = 3, x4 = 4, x5 = 5 like I want
Thank you,
Kevin
0 Kommentare
Akzeptierte Antwort
Deepak Ramaswamy
am 14 Mai 2014
Bearbeitet: Deepak Ramaswamy
am 14 Mai 2014
I really don't like this solution at all but does this work for you?:
syms x1 x2 x3 x4 x5
X = [x1 x2 x3 x4 x5].';
values = [1 2 3 4 5].';
for k = 1:numel(X)
eval(sprintf('%s=values(%d)',char(X(k)),k));
end
This creates x1 x2 etc in the MATLAB workspace
2 Kommentare
Deepak Ramaswamy
am 14 Mai 2014
I agree using matlabFunction() whenever the symbolic expression is "set" (i.e. there is no need for further symbolic operations) will provide better performance.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Symbolic Variables, Expressions, Functions, and Preferences finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!