Deleting elements from a symbolic array
6 views (last 30 days)
Show older comments
I have a symbolic array like:
symarray = sym('[pi/2 a1 0]')
and I want to delete symbolic constant element (like 'pi' and '0').
I thought at first to apply a trigonometric function to whole array, like
S=sin(symarray);
In this way not-constant elements will change in sin-of, like
S =
[1,sin(a1),0];
So, converting S-vector to a char cell I can find elements such that the first three letters correspond to 'sin'. So I do:
for ii=1:length(symarray)
symcharvect{ii}=char(S(ii)); %sym-to-char conv.
if strncmp(symcharvect{ii},'sin',3)==0;%check if ii-string start with 'sin..'
symarray(ii)=[]; %delete ii-constant-element
end
end
but it doesn't work and returns this error:
Error using mupadmex
Error in MuPAD command: Subscripted assignment dimension mismatch
Error in sym/subsasgn (line 1638)
C = mupadmex('symobj::subsasgn',A.s,B.s,inds{:});
I think that the problem is in ii-indexing.
any suggestions?
0 Comments
Accepted Answer
Cedric
on 22 Jan 2013
Edited: Cedric
on 22 Jan 2013
An alternative could be to work on the initial string that you pass to sym().
For example, if all your parameters were named a# where # is a positive integer, you could do something like:
>> str = '[pi/2 a1 0 a22 5 e -3 a8 a789]' ; % Test string.
>> m = regexp( str, 'a\d+\s?', 'match' ) ;
>> str = ['[', [m{:}],']']
str = [a1 a22 a8 a789]
The 2nd arg in the call to regexp() is the pattern that you want to match. Here it tells regexp to match the literal 'a' followed by one or more ( + after \d ) decimal digits ( \d ), followed by zero or one ( ? after \s ) whitespace character.
Cheers,
Cedric
0 Comments
More Answers (1)
Walter Roberson
on 22 Jan 2013
Edited: Walter Roberson
on 22 Jan 2013
Suppose you deleted element #3. Then #4 "falls down" into where #3 was, #5 falls to where #4 was, and so on. So afterwards, the array is one shorter. Meanwhile, "for" only ever evaluates the loop bounds you give once, when the loop is first started, so if there were (say) 12 items in the array originally, the "for" is going to go to 12 even though there might not be 12 items left by the time you try to access #12.
You are better off figuring out which entries to delete and deleting them all at once. Or use a "while" loop instead of "for" (but watch out that after deleting #3 it would be #3 that you would have to examine next because #4 dropped down to #3.) Or, and here's a trick: loop backwards from length(symarray) to 1.
A much better way to achive your purpose is:
symarray := feval(symengine, 'select', 'hastype', symarray, 'Type::Indeterminate');
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!