Azzera filtri
Azzera filtri

Create an symbol array

11 visualizzazioni (ultimi 30 giorni)
Juan Miguel
Juan Miguel il 1 Dic 2011
Modificato: Karan Gill il 21 Feb 2017
Hello!!
I want to create a symbolic matrix in my Matlab( version R2010a), I have searched how could do it. I have seen this sentence:
A = sym('A', [3 4])
But when I write it in my Matlab, I receive this message:
??? 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);
I don't know why this sentence doesn't work when I have got it from a Matlab Document.
Could someone help me?

Risposte (3)

Andrei Bobrov
Andrei Bobrov il 1 Dic 2011
long way :)
m = 3;
n = 4;
[a b] = ndgrid(1:m,1:n);
p1 = regexp(sprintf('A_%d_%d,',[a(:)';b(:)']),',','split');
p2 = cellfun(@sym,p1(1:end-1),'un',0);
A = reshape([p2{:}],m,[])
  1 Commento
Karan Gill
Karan Gill il 21 Feb 2017
Modificato: Karan Gill il 21 Feb 2017
Starting R2010b, this is wrong. See my answer below.

Accedi per commentare.


Karan Gill
Karan Gill il 21 Feb 2017
Modificato: Karan Gill il 21 Feb 2017
Starting R2010b
>> A = sym('A',[2 3])
A =
[ A1_1, A1_2, A1_3]
[ A2_1, A2_2, A2_3]
>> A = sym('A%d%d',[2 3])
A =
[ A11, A12, A13]
[ A21, A22, A23]
>> A = sym('A_%d%d%d',[2 3 2])
A(:,:,1) =
[ A_111, A_121, A_131]
[ A_211, A_221, A_231]
A(:,:,2) =
[ A_112, A_122, A_132]
[ A_212, A_222, A_232]

Steven Lord
Steven Lord il 21 Feb 2017
This syntax is listed in the documentation online, which is the documentation for the most recent release (currently release R2016b.) But that documentation includes functionality that is not present in your release, such as the ability to create symbolic matrices. The Release Notes indicate that functionality was introduced in release R2010b (see the first item in the section of the Release Notes to which I linked.)
I don't believe there is a way to create a symbolic matrix like Karan showed in your release, short of manually creating the elements and concatenating them (as the "Creating Symbolic Variables and Expressions" page in the Getting Started section of the Symbolic Math Toolbox documentation for release R2010a shows.)
syms a b c;
A = [a b c; c a b; b c a]
If possible I recommend upgrading your software as there have been many enhancements to both MATLAB and Symbolic Math Toolbox in the past seven years or so. You can see a list of some of those enhancements if you change the upper element of the Release Range on the Release Notes page to which I linked to R2016b.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by