Problem in syntax

Please help!!!
a lok = sym('alok%d%d', [3 5 1]) % the piece of code and the error is below
??? Error using ==> sym.sym>createCharMatrix at 2480
Matrix syntax can only create vectors and matrices.
Error in ==> sym.sym>convertCharWithOption at 2439
s = createCharMatrix(x,a);
Error in ==> sym.sym>tomupad at 2195
S = convertCharWithOption(x,a);
Error in ==> sym.sym>sym.sym at 123
S.s = tomupad(x,a);

 Accepted Answer

Walter Roberson
Walter Roberson on 22 Oct 2011

0 votes

You cannot use that syntax to create a 3D symbolic matrix.
I note that you only have two %d in your symbol format, so I am not sure what you want to have happen for the third index?

8 Comments

Alok
Alok on 22 Oct 2011
hello walter, thanks for replying, I am sorry for missing 1 %d and is there any way to generate 3D symbolic matrix ?
The method I give in a previous answer might be suitable for your purpose:
http://www.mathworks.com/matlabcentral/answers/2521-creating-vector-of-symbols
Alok
Alok on 22 Oct 2011
Oh thanks man,
I forgot about ndgrid, thanks again buddy, now my solver can be fully developed :)
Alok
Alok on 22 Oct 2011
according to your note that you referred, will it help in 2011a version ?
2011a and 2011b can use sym('pattern', [m n]) but that syntax does not extend to 3 or more dimensions.
Alok
Alok on 23 Oct 2011
hello walter,
I tried to apply the code which you suggested, it was quite nice but it is giving me the following error, is it possible for you to look into the errors ?
>> P = 10; Q = 10; R = 10;
[ x, y, z ] = ndgrid( 1 : P, 1 : Q, 1 : R );
>> A = reshape( cellfun( @sym, strcat( 'A', cellstr( num2str( x( : ) ) ), cellstr( num2str( y( : ) ) ), cellstr( num2str( z( : ) ) ) ), 'Uniform', 0 ), P, Q, R );
??? Error using ==> sym.sym>convertExpression at 2547
Error: Unexpected 'integer' [line 1, col 3]
Error in ==> sym.sym>convertChar at 2458
s = convertExpression(x);
Error in ==> sym.sym>convertCharWithOption at 2441
s = convertChar(x);
Error in ==> sym.sym>tomupad at 2195
S = convertCharWithOption(x,a);
Error in ==> sym.sym>sym.sym at 111
S.s = tomupad(x,'');
Ah, I didn't allow for multiple digits in the range. That's a bit of a nuisance.
Warning: this code will not work properly if you attempt to use a dimension of 0.
Pd = ceil(log10(P+1)); Qd = ceil(log10(Q+1)); Rd = ceil(log10(R+1));
Pf = sprintf('%%0%dd', Pd); Qf = sprintf('%%0%dd', Qd); Rf = sprintf('%%0%dd', Rd);
A = reshape( cellfun( @sym, strcat( 'A', cellstr(num2str(x(:),Pf)), cellstr(num2str(y(:),Qf)), cellstr(num2str(z(:),Rf)) ), 'Uniform', 0 ), P, Q, R );
This will add leading 0's to the values that are shorter.
The above code does not use any separation between the indices. If you want something such as an underscore, put it before the close-quote in Pf and Qf (but not in Rf).
If you want a separator _and_ you want the numbering to be as short as fits rather than using leading 0's, then you would not need Pd, Qd or Rd, and you would use
Pf = '%-d_'; Qf = '%-d_'; Rf = '%-d';
I think. I'd want to test that out to be safe (unfortunately my server is still unreachable these days.)
Alok
Alok on 29 Oct 2011
Hello walter, I checked your solution after a while, could you please explain me, what is x(:), y and z, in the reshape function ?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!