Basic math operations on contents of a cell array

조회 수: 95 (최근 30일)
Bill Tubbs
Bill Tubbs 2020년 11월 4일
댓글: Bill Tubbs 2020년 11월 4일
Is there any reason why basic math operations don't work with cell arrays? Seems to me it would extremely useful and efficient if you could do the following:
C = [0.7152 1.0000];
A = [0.6807 0; 0 1.0000];
Apows = {A,A^2,A^3};
C*Apows
Result:
Undefined operator '*' for input arguments of type 'cell'.
Expected:
{C*Apows{1},C*Apows{2},C*Apows{3}} =
1×3 cell array
{1×2 double} {1×2 double} {1×2 double}
Is this the only/best way to do this (which I got from this answer):
cellfun(@(x) C*x,Apows,'UniformOutput',false);
ans =
1×3 cell array
{1×2 double} {1×2 double} {1×2 double}

채택된 답변

Matt J
Matt J 2020년 11월 4일
편집: Matt J 2020년 11월 4일
Is there any reason why basic math operations don't work with cell arrays? Seems to me it would extremely useful and efficient if you could do the following:
Cell arrays are designed for containing objects of different types and sizes. They are a fundamentally inefficient way to hold objects that are all the same size, partly because their contents are not guaranteed to be held contiguously in RAM. Because a better data type is usually available for the situations you describe, TMW hasn't bothered to define such operations for cells. You could write cell-like class with overloaded arithmetic operations if you wished, but it would be syntactic sugar, and nothing more.
For the scenario in your example, moreover, the best method is in any case a simple for loop. This accomplishes the whole operation with just 3 multiplications with A, unlike the approach you presented which first forms Apow.
out=cell(1,4);
out{1}=C;
for i=2:4
out{i}=out{i-1}*A;
end
out=out(2:end);
  댓글 수: 4
Bill Tubbs
Bill Tubbs 2020년 11월 4일
Thanks, that answers the question!
Bill Tubbs
Bill Tubbs 2020년 11월 4일
It could be argued that the applicability of cellfun is so great that it could be replaced with a dedicated language feature. E.g. something similar to list comprehensions in Python. Maybe something like this:
{C*x for x=Apows}
(would produce a cell array)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

James Tursa
James Tursa 2020년 11월 4일
I suppose only TMW could give you their reasons for not implementing numeric operations on cell arrays. Maybe they simply didn't want to expend the effort when cellfun( ) would suffice.
One other option for you is to stack your matrices in a single array:
Apows = cat(3,A,A^2,A^3);
Then you can use a variety of options for doing the page matrix multiplies:
pagemtimes (R2020b)
Some options from the FEX:
MULTIPROD:
MTIMESX: (C-mex code requiring C compiler) needs updated build routine for later MATLAB versions
MMX: (C-mex code requiring C compiler)
  댓글 수: 1
Bill Tubbs
Bill Tubbs 2020년 11월 4일
Thanks, these alternatives to using cellarrays seem to be exactly what I am actually looking for in this case where the computations are matrix operations. I will look into these...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by