フィルターのクリア

how do i replace part of Symbolic Matrix ?

2 ビュー (過去 30 日間)
timo
timo 2016 年 3 月 28 日
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]
  3 件のコメント
timo
timo 2016 年 3 月 29 日
clear
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;
R11 = R1+R2
R22 = R1+R2+R3
R33 = 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],[R11 R22 R33])
I get an error at the end -_-''
Christopher Creutzig
Christopher Creutzig 2016 年 4 月 4 日
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?

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2016 年 3 月 29 日
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.
clear
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 件のコメント
timo
timo 2016 年 3 月 29 日
編集済み: timo 2016 年 3 月 29 日
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

サインインしてコメントする。

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 28 日
編集済み: Azzi Abdelmalek 2016 年 3 月 28 日
delta = sym('R%d%d',[3,3])
v=sym('R',[3 1])
idx=sub2ind(size(delta),1:3,1:3])
delta(idx)=v

Walter Roberson
Walter Roberson 2016 年 3 月 29 日
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
syms('x')
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.
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 3 月 30 日
Timo comments that this is an "off-topic wall of text response to a question that has already been answered"
Walter Roberson
Walter Roberson 2016 年 3 月 30 日
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.)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by