Deleting elements from a symbolic array
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?
Accepted Answer
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');
1 Comment
Marco
on 22 Jan 2013
Categories
Find more on Common Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!