Overloading math operations on cell arrays
조회 수: 2 (최근 30일)
이전 댓글 표시
It is easy to add two cell arrays together with a function that indexes into each array, and adds. Of course the arrays must be compatible. The advantage is that they don't have to be uniform. For example, one might combine scalars and a matrix, like {1,[2,3;4,5],6}, and two cell arrays with compatible entries have an obvious sum.
Is it possible to define an overloaded addition (etc) to add any two such compatible cell arrays?
Some people would say it is unnecessary, but it would shorten my code, and improve readability. I don't want to define a class, but just add cell arrays without fuss. Of course, I can do it through function calls, but the result is longer and less readable. This may be generally useful in other applications with nonhomogeneous arrays.
댓글 수: 1
James Tursa
2022년 6월 8일
I suppose you might be able to find the correct subfolder location to put a plus.m file that acts on cell arrays, but this practice is not advised because now every program will see it and maybe you will break some existing code that depends on this functionality not working in this way.
답변 (2개)
the cyclist
2022년 6월 8일
A = {1,[2,3;4,5],6};
B = {2,[3,4;5,6],7};
C = cellfun(@(x,y)(x+y),A,B,'UniformOutput',false)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!