How to instantly substitute all the Ai_j of a symbolic matrix A?

조회 수: 2 (최근 30일)
Lucas Medina
Lucas Medina 2015년 7월 9일
편집: Lucas Medina 2015년 7월 9일
I know that I can create a symbolic matrix A in the following way:
>> A = sym('A', [3 3], 'real')
A =
[ A1_1, A1_2, A1_3]
[ A2_1, A2_2, A2_3]
[ A3_1, A3_2, A3_3]
Then I can, for example create a matrix representing the inverse of A:
>> Ai = inv(A)
Ai =
[ (A2_2*A3_3 - A2_3*A3_2)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1), -(A1_2*A3_3 - A1_3*A3_2)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1), (A1_2*A2_3 - A1_3*A2_2)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1)]
[ -(A2_1*A3_3 - A2_3*A3_1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1), (A1_1*A3_3 - A1_3*A3_1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1), -(A1_1*A2_3 - A1_3*A2_1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1)]
[ (A2_1*A3_2 - A2_2*A3_1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1), -(A1_1*A3_2 - A1_2*A3_1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1), (A1_1*A2_2 - A1_2*A2_1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1)]
Say I wanted to evaluate Ai for [1 2 3; 5 4 6; 7 8 9]. How can I substitute all the Ai_j without using subs() one by one on each entry in A? I'd like to be able to substitute the new A as a whole without having to substitute each of its entries.
  댓글 수: 1
Lucas Medina
Lucas Medina 2015년 7월 9일
편집: Lucas Medina 2015년 7월 9일
I used those numbers as an example. What I actually want to do is work with random matrices of varying dimensions, which means I won't know what the values are or how many to put in. That approach would be useless in this case.

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

채택된 답변

Steven Lord
Steven Lord 2015년 7월 9일
SUBS lets you specify a symbolic matrix for substitution. See the description of the input argument named "old" (particularly the types of inputs accepted for that argument) in the Inputs section of its documentation page.
A = sym('A', [3 3], 'real');
B = A^2; % I don't recommend using INV, so I'll use squaring instead
M = reshape(randperm(9), 3, 3); % arbitrary data to be substituted
s1 = subs(B, A, M);
s2 = M^2;
isequal(double(s1), s2) % should return true
  댓글 수: 1
Lucas Medina
Lucas Medina 2015년 7월 9일
Exactly what I wanted. Thanks. Why wouldn't you recommend inv? How else would you take the inverse of a matrix?

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 9일
A = sym('A', [3 3])
Ai=inv(A)
a=[1 2 3; 5 4 6; 7 8 9]
arrayfun(@(x,y) assignin('base',char(x),y),A,a)
double(subs(Ai))

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by