Creating dynamic number of Symbolic variables

14 views (last 30 days)
Hi,
(I had some code which was running fine in Matlab 2014a but after upgrading to 2015a today, the same code is not working)
I want to create a symbolic variable t and it's value to contain further symbolic variables called as t1, t2, t3 and so on, wherein the number of these symbolic variable to be determined by a user provided value stored under "NumOfExpVar"
I wrote the below code and it works perfectly in Matlab 2014a and also in 2015a.
NumOfExpVar = 1; % This value will be set by user
t = sym(zeros(1, NumOfExpVar));
for NEVCnter=1:NumOfExpVar
t(NEVCnter) = sym(sprintf('t%d', NEVCnter));
end
So far so good.
Now, after a long series of intermediate code (which has nothing to do with "t" symbolic variables) I have another code wherein for each of t1, t2 etc I want to create symbolic variables x1_1, x1_2, x1_3...(stored under t1), x2_1, x2_2, x2_3 etc (stored under t2) wherein the number after "_" is determined by "ExpRunCnter". The code I have is
ExpRunCnter = 2; %This value is set dynamically depending on state of the program
for NEVCnter = 1:NumOfExpVar
t(NEVCnter) = sym(zeros(1, ExpRunCnter));
for ERCCnter = 1:ExpRunCnter
t(NEVCnter,ERCCnter) = sym(sprintf('x%d_%d', NEVCnter, ERCCnter));
end
end
Unfortunately the above does NOT work in 2015a (but did work in 2014a)
Please let know.
Thanks
Hari
PS: the error I get in 2015a is as follows (error pointing to the line "t(NEVCnter) = sym(zeros(1, ExpRunCnter));":
Error using subsasgn In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in sym/privsubsasgn (line 997) L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 834) C = privsubsasgn(L,R,inds{:});

Accepted Answer

Walter Roberson
Walter Roberson on 31 Jul 2015
For several releases now you have been able to use sym() to generate the names directly.
A = sym('a',[m,n]) creates an m-by-n symbolic matrix filled with automatically generated elements. The generated elements do not appear in the MATLAB workspace.
When you use this syntax to create a vector, it generates the elements by using the prefix a and attaching the numbers from 1 to m or n to it. For example, A = sym('a',[1,3]) creates a row vector A = [a1,a2,a3].
When you use this syntax to create a matrix, it generates the elements of the form ai_j, where i = 1:m and j = 1:n. For example, A = sym('a',[2 2]) generates the 2-by-2 symbolic matrix A = [a1_1, a1_2; a2_1, a2_2].
To specify another form for generated names of matrix elements, use combinations of '%d' and the prefix a. For example, A = sym('a_%d',[1 3]) generates a row vector A = [a_1, a_2, a_3], and AB = sym('a%db%d',[2 2]) generates the 2-by-2 symbolic matrix AB = [a1b1, a1b2; a2b1, a2b2].
  1 Comment
Hari
Hari on 31 Jul 2015
Hello Walter,
Thanks for the response and letting me know about a simpler approach. It certainly helps me to build the matrix in a straight-forward manner.
Thanks Hari

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!