v =
How do I convert symbolic values from a matrix to numeric?
Show older comments
I used a matrix to create symbolic values.
syms v [n,1]
% Creates a matrix of height n with variables v1, v2...vn
%v =
%
% v1
% v2
% vn
%
% etc
I later use those variables to create other equations and then want to solve. The problem is that I am creating n variables and I can't set v1=1, v2=2, etc since n will be different each time it runs and I can't just refer to v(1), v(2), or v(n) since that changes the value of the matrix element, not the symbolic variable v1, v2, or vn. How do I set v1, v2, and everything up to vn equal to a numeral value?
2 Comments
Image Analyst
on 26 Feb 2025
Why do you need symbolic variables at first and then need to digitize them? Why can't you just start with normal non-symbolic variables right from the start?
Alan
on 26 Feb 2025
Answers (1)
n = 5;
syms v [n,1]
v
v(1)
So indexing v does work to return the corresponding symbolic variable.
Meanwhile, to substitute for each v value,
f = v(1)^2 + v(2) - v(3)
subs(f, v, [3 -1 2 4 2].')
2 Comments
Alan
on 26 Feb 2025
subs(EXPRESSIONS, v, VECTOR_OF_VALUES)
will process EXPRESSIONS substituting in all of the values in v with their corresponding numerical values and returning the modified version of EXPRESSIONS.
There is no way to say that you want the variables to be substituted with their corresponding values for all symbolic expressions .
For example,
syms x y
f1 = x^2 + y
f2 = x^3 - y^2 + x*y
X = 2; Y = 5;
%this is possible
new_f1_f2 = subs([f1, f2], {x y}, {X Y})
%this does not work as you might hope
x = X; y = Y;
f1
f2
%this also does not work as you might hope
subs({x; y}, {X Y})
f1
f2
There is no way to say "subsitute these values for x and y" and have it affect expressions you did not mention in the command -- no way to affect (say) f1 unless you include f1 in the list of expressions to be processed.
This is fundamental to the way that the Symbolic Engine works, so it is not going to be changed.
Categories
Find more on Calculus 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!





