add all the same-sized symbol matrices in a cell array
Version 1.0.0 (1,16 KB) von
Wan Ji
adding matrices inside cell array
Actually, the same-sized numeric matrices in a cell array can be easily summarized by using
% Create dummy input
c = {[1 2; 3 4], [3 3; 1 1]}
c =
[2x2 double] [2x2 double]
% Elementwise sum across matrices (only if ALL matrices have same size)
sum(cat(3,c{:}),3)
ans =
4 5
4 5
However, it does not work for symbol matrices in a cell.
syms x
q = [x,2*x,3*x;sin(x),cos(x),tan(x);exp(x),2^x,log(x)];
c = arrayfun(@(i)q,(1:10)',...
'UniformOutput' ,false);
sum(cat(3,c{:}),3)
错误使用 symengine
Arguments must be 2-dimensional.
出错 sym/privUnaryOp (line 1036)
Csym = mupadmex(op,args{1}.s,varargin{:});
出错 sym/sum (line 85)
s = privUnaryOp(A, 'symobj::prodsumdim', num2str(dim), '_plus');
Here the function is used for this purpose. Although matlab fixed the bug in 2021a and later.
We have
b = symCellSum(a)
And then the answer is obtained
b =
[ 10*x, 20*x, 30*x]
[ 10*sin(x), 10*cos(x), 10*tan(x)]
[ 10*exp(x), 10*2^x, 10*log(x)]
Enjoy for whose matlab version lower than 2020a
Zitieren als
Wan Ji (2025). add all the same-sized symbol matrices in a cell array (https://de.mathworks.com/matlabcentral/fileexchange/95728-add-all-the-same-sized-symbol-matrices-in-a-cell-array), MATLAB Central File Exchange. Abgerufen.
Kompatibilität der MATLAB-Version
Erstellt mit
R2020a
Kompatibel mit R2009a bis R2020b
Plattform-Kompatibilität
Windows macOS LinuxTags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Live Editor erkunden
Erstellen Sie Skripte mit Code, Ausgabe und formatiertem Text in einem einzigen ausführbaren Dokument.
symCellSum
| Version | Veröffentlicht | Versionshinweise | |
|---|---|---|---|
| 1.0.0 |
