Are Arithmetic Operations on a Cell Array of sym Objects Documented Behavior?

1 visualizzazione (ultimi 30 giorni)
I inadvertently stumbled across this behavior:
syms z
H(z) = 1/(z+1) - 1/(z+2)
H(z) = 
c = children(H(z))
c = 1×2 cell array
{[1/(z + 1)]} {[-1/(z + 2)]}
whos c
Name Size Bytes Class Attributes c 1x2 224 cell
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
ans = 
Note that the result is returned as an array of sym, not a cell array of sym
whos ans
Name Size Bytes Class Attributes ans 1x2 8 sym
So this works, which is actually what I wanted to do
sum(c*z)
ans = 
Elementwise operation works as well
c + [z 2*z] % element wise addition
ans = 
c .* [z 2*z] % elemetwise multiplication
ans = 
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 = 1×2 cell array
{[1]} {[2]}
c = mat2cell(double(c*sym(2)),1,ones(1,numel(c)))
c = 1×2 cell array
{[2]} {[4]}
  3 Commenti
Paul
Paul il 23 Ott 2022
I was just surprised that this doesn't throw an error
syms z
c = {z , z};
c + z^2
ans = 
when this does
c = {1 ,2};
c + 2
Operator '+' is not supported for operands of type 'cell'.
So that error message isn't actually correct, as there are cases where operator + is supported when an operand is of type cell as in the first case, for which I can't find any supporting documentation.
dpb
dpb il 24 Ott 2022
syms z
c = {z , z};
c + z^2
ans = 
class(ans)
ans = 'sym'
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).

Accedi per commentare.

Risposte (0)

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by