Assign number to symbol in cell array

4 ビュー (過去 30 日間)
Jongwook
Jongwook 2014 年 9 月 20 日
回答済み: Abhiram Bhanuprakash 2014 年 9 月 29 日
Hello,
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?

回答 (1 件)

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