Assign number to symbol in cell array

조회 수: 12(최근 30일)
Jongwook 2014년 9월 20일
답변: Abhiram Bhanuprakash 2014년 9월 29일
I want to assign a number (ex. 2) to a symbolic variable (x) in a cell array (A) using a cell array index operator {}.
syms x y ... ; A{1}=x; A{2}=y; ...
For example, if I write A{1}=2, then A{1} changes to 2; x does not become 2, definitely. Is there any way?


Abhiram Bhanuprakash
Abhiram Bhanuprakash 2014년 9월 29일
The behaviour which you have pointed out is expected, because you are setting the value of the cell array element A{1} to 2, but not setting a value to the symbol. Assigning a 'double' value to a 'sym' variable would just convert the 'sym' variable to a 'double'.
You can use the function SUBS to substitute a symbolic variable with a 'double' in MATLAB.
Here is some sample code to do the same:
>> syms x,y;
>> A{1} = x; A{2} = y;
>> subs(A{1}+A{2},A{1},5);
You would get the result to be
y + 5

Community Treasure Hunt

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

Start Hunting!

Translated by