Creating vector of symbols

조회 수: 12 (최근 30일)
Brian
Brian 2011년 3월 4일
댓글: Walter Roberson 2017년 3월 2일
For example, A = sym('A%d%d', [3 3]) generates the 3-by-3 symbolic matrix A with the elements A11, A12, ..., A33

채택된 답변

Walter Roberson
Walter Roberson 2011년 3월 4일
편집: Walter Roberson 2017년 2월 22일
For example, for a P x Q x R matrix,
[x,y,z]=ndgrid(1:P,1:Q,1:R);
A = sym( reshape( ...
cellfun(@sym, ...
strcat('A', ...
cellstr(num2str(x(:))), ...
cellstr(num2str(y(:))), ...
cellstr(num2str(z(:)))), ...
'Uniform', 0), ...
P, Q, R ) );
The code is not as long as it looks; I formatted it for display purposes.
Trim to two dimensions or expand to the number of dimensions needed.
  댓글 수: 4
Karan Gill
Karan Gill 2017년 3월 2일
No, >2 dim works in R2016a (unless I misunderstand you).
>> A = sym('A', [2 2 2])
A(:,:,1) =
[ A1_1_1, A1_2_1]
[ A2_1_1, A2_2_1]
A(:,:,2) =
[ A1_1_2, A1_2_2]
[ A2_1_2, A2_2_2]
Walter Roberson
Walter Roberson 2017년 3월 2일
Ah, then I think that advance is best left as a comment rather than by editing the response which was appropriate for the time it was written and continues to be required by people using older versions.

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

추가 답변 (3개)

Walter Roberson
Walter Roberson 2011년 3월 4일
A = sym('A%d', [3 1]);
or just
A = sym('A', [3 1]);

Brian
Brian 2011년 3월 4일
The first gives the following errors
??? Error using ==> sym.sym>expression2ref at 2408 Error: Unexpected '
Error in ==> sym.sym>char2ref at 2378 s = expression2ref(x);
Error in ==> sym.sym>tomupad at 2147 S = char2ref(x);
Error in ==> sym.sym>sym.sym at 114 S.s = tomupad(x,a);
and the second gives.
??? Error using ==> error Formatted arguments cannot be non-scalar numeric matrices.
Error in ==> sym.sym>assumptions at 2180 error('symbolic:sym:sym:errmsg1','Second argument %s not recognized.',a);
Error in ==> sym.sym>tomupad at 2148 assumptions(S,x,a);
Error in ==> sym.sym>sym.sym at 114 S.s = tomupad(x,a);
  댓글 수: 3
Brian
Brian 2011년 3월 4일
Ah I have 7.10.0 (R2010a). I take it that's the problem then...
Walter Roberson
Walter Roberson 2011년 3월 4일
It does appear that supplying a size is a new feature as of 2010b; the 2010a documentation does not show it.
http://www.mathworks.com/help/releases/R2010a/toolbox/symbolic/sym.html

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


Paulo Silva
Paulo Silva 2011년 3월 4일
A=[3 3]; %create any numeric vector or matrix
A=sym(A) %convert it to symbolic
A=double(A) %convert it back to numeric (double in this case)
  댓글 수: 2
Brian
Brian 2011년 3월 4일
Can numbers be converted to symbols?
If you do something like...
A=[1 2]
A = sym(A)
A(1) + 1
Then you get 2 as an answer.
Paulo Silva
Paulo Silva 2011년 3월 4일
One thing you should always have open in your matlab is the Workspace window so you can see what variables are create and their details, other ways is to do
class(A) or in the case of your small code class(ans).
In a way they can, that small code of yours gives the ans=2 but it's a symbolic response not numeric. My1=sym('1'); 2*My1 gives ans=2, again it's a symbolic value. There are other ways to make symbolic values but you should look first in your documentation.

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

카테고리

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