MATLAB Answers

how do i replace part of Symbolic Matrix ?

4 views (last 30 days)
If I have Matrix
delta = sym('R%d%d',[3,3])
[ R11, R12, R13]
[ R21, R22, R23]
[ R31, R32, R33]
If i want to replace R11 R22 and R33 with [1 2 3] i tried
subs(delta,[R11 R22 R33],[1 2 3])
This doesnt replace anything althought for 1d array it works
delta = sym('R%d',[1,3])
subs(delta,[R1 R2 R3],[1 2 3])
ans =
[ 1, 2, 3]
Christopher Creutzig
Christopher Creutzig on 4 Apr 2016
It is not really recommended to mix symbolic variables and numerical variables with the same name, although there is some support for doing so. Does subs(delta) give you the expected answer?

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 29 Mar 2016
Based on the information in the second comment, I know what's going on. By the time MATLAB reaches the SUBS call, the variables R11, R22, and R33 are no longer symbolic and as such are no longer valid for use in the second input argument. One solution is to explicitly create symbolic variables inside the SUBS call:
subs(delta,{sym('R11') sym('R22') sym('R33')},{R11 R22 R33})
Another is to avoid overwriting the symbolic variables R11, R22, and R33 in the first place.
syms R11 R22 R33 R1 R2 R3 R4 R5 R11 R12 R13 R21 R23 R31 R32
R1 =9;R2=4; R3=6;R4=3;R5=6;
R11D = R1+R2;
R22D = R1+R2+R3;
R33D = R3+R4+R5;
R21 = R1 + R2; R12 =R21;
R13 = 0; R31 =R13;
R23= R3; R32 =R23;
signsMat = [1 -1 -1;
-1 1 -1;
-1 -1 1];
delta = sym('R%d%d',[3,3]).*signsMat;
subs(delta,{R11 R22 R33},{R11D R22D R33D})
You'd want to do the same with the off-diagonal elements if you later wanted to substitute them into delta.
  1 Comment
timo on 29 Mar 2016
R11D ... ETC are double like in my example ... R11 R22 R33 ... that should work ? Anyway creating extra variables will make the code very hard to read. The first solution seems acceptable ...barely

Sign in to comment.

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 28 Mar 2016
Edited: Azzi Abdelmalek on 28 Mar 2016
delta = sym('R%d%d',[3,3])
v=sym('R',[3 1])

Walter Roberson
Walter Roberson on 29 Mar 2016
Once you have used a symbolic variable in an expression, assigning a new value to the variable almost always leads to confusion and bugs. The situation is very much like
A = 1
B = A + 1
A = 2
and then expecting that B will have changed from 2 to 3 because B was assigned in terms of A and A has changed. It does not work that way numerically and it does not work that way symbolically: every use of a symbolic variable takes a copy of its current value.
MATLAB almost never goes by variable name. Even
syms x
is not going by variable name: instead it is making use of command / function equivalence and calling
which is (for this purpose) effectively defined as
function syms(name)
assignin('caller', name, sym(name));
which is to say, almost equivalent to
x = sym('x');
After that, every reference to x would resolve to its current value, which would be sym('x') until it was reassigned. But as soon as you reassign a numeric value to it, then the references to x resolve to the numeric value instead, including in subs() . This is normal MATLAB processing, and it is necessary. For example if you define
syms R1, R2, R3
vars = [R1, R2, R3];
subs(Expression, vars, [5, 6, 8])
then you need vars to resolve to its value which is the vector of names: you would not want vars to be treated as sym('vars') just because it appears in the second position in a subs() call.
Remember, pretty much every reference to a variable (that is not on the left side of an assignment) is to be resolved by value.
If "The first solution seems acceptable ...barely" and avoiding assigning values over top of symbol names is not acceptable at all, then you need to find yourself a different programming language -- one that is not MATLAB, Scilab, Octave, Maple, fortran, C, C++, or perl. As I recall you would also need to avoid Mathematica, and IDL, but it has been some time since I have used those so I could be forgetting some of their quirks.
Walter Roberson
Walter Roberson on 30 Mar 2016
timo, a response that tells you why you had a problem when you did not understand the issues, is not off topic.
Respondents are also free to post better or different Answers to Questions emphasizing different understanding. The fact that the Question has been answered already is not especially relevant: different people have different interpretations and bring different aspects to the discussion.
Remember too that all of this discussion is public for a reason: the Questions and Answers are left accessible for other people to study and learn from. If someone else other than you reads my response and learns from it, then its purpose will have been served (though it would be even better if you learned from it as well.)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!