Are Arithmetic Operations on a Cell Array of sym Objects Documented Behavior?
1 view (last 30 days)
Show older comments
I inadvertently stumbled across this behavior:
syms z
H(z) = 1/(z+1) - 1/(z+2)
c = children(H(z))
whos c
c is a cell array of sym objects. At this point, I thought that in order to operate on the elements of c I'd have to first extract them from c. However, that's not the case here
c*z % multiplies each element of c by z
Note that the result is returned as an array of sym, not a cell array of sym
whos ans
So this works, which is actually what I wanted to do
sum(c*z)
Elementwise operation works as well
c + [z 2*z] % element wise addition
c .* [z 2*z] % elemetwise multiplication
Is this very useful behavior documented anywhere (I couldn't find it)? I'd like to know what operations are supported on cellsym (is that the right term) arrays.
These results also suggest a way to operate on numeric cell arrays without using cellfun.
c = {1,2}
c = mat2cell(double(c*sym(2)),1,ones(1,numel(c)))
3 Comments
dpb
on 24 Oct 2022
syms z
c = {z , z};
c + z^2
class(ans)
That's easy -- the cell is promoted to 'sym' before the addition. That's basic rule that nothing is declared and of an immutable class on assignment and the dynamic allocation in MATLAB. It's the Symbolic TB taking over with the overloaded operators that makes it transparent (mostly).
Answers (0)
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!