필터 지우기
필터 지우기

Assign Values to Each Element of Symbolic Array

조회 수: 4 (최근 30일)
Kevin Bachovchin
Kevin Bachovchin 2014년 5월 12일
댓글: Deepak Ramaswamy 2014년 5월 14일
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

채택된 답변

Deepak Ramaswamy
Deepak Ramaswamy 2014년 5월 14일
편집: Deepak Ramaswamy 2014년 5월 14일
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
Kevin Bachovchin
Kevin Bachovchin 2014년 5월 14일
That does work. However, if I want to substitute in numerical values in expressions that contain x1, x2, etc, I need to use the "subs" command.
NewExpr = subs(OldExpr, X, values)
The "subs" command is pretty inefficient from what I see though. I think I'm better off creating a function handle from the symbolic expression using the "matlabFunction" command.
Deepak Ramaswamy
Deepak Ramaswamy 2014년 5월 14일
I agree using matlabFunction() whenever the symbolic expression is "set" (i.e. there is no need for further symbolic operations) will provide better performance.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by