'deal' doesn't distribute the elements of a calculated array out of cellfun

조회 수: 3(최근 30일)
For the following
syms syma symb
eqns = [...
syma*exp(symb)== 1,...
syma*exp(symb*1500) == 5,...
vars = [syma symb ];
[sola, solb ] = deal(cellfun(@(C) double(C),struct2cell(vpasolve(eqns,vars)),'UniformOutput',true)')
the ouput is
sola = 0.9989 0.0011
solb = 0.9989 0.0011
while, expectedly, sola should take only the first value 0.9989 and solb should take inly the second value 0.0011.

채택된 답변

Walter Roberson
Walter Roberson 2020년 11월 27일
deal has never done that for any kind of variable.
You are calculating a single vector in the cellfun, so your code with deal() comes down to:
[A,B] = deal([9 11])
A = 1×2
9 11
B = 1×2
9 11
deal() has two functions:
%assign the same value to multiple variables
[A, B] = deal(9)
A = 9
B = 9
%historic. split multiple outputs between multiple variables:
C = num2cell([9 11]);
[A, B] = deal(C{:})
A = 9
B = 11
%these days you can do that without the deal() call:
C = num2cell([10 12]);
[A, B] = C{:}
A = 10
B = 12
Neither of those modes splits a vector into multiple output variables.
Expand = @(C) C{:};
Split = @(V) Expand(num2cell(V));
[A, B] = Split([11 13])
A = 11
B = 13
  댓글 수: 6
Walter Roberson
Walter Roberson 2021년 6월 23일
The amount of thought needed to get the correct solution using subsref() far exceeds any execution time you might hypothetically gain by using subsref() for this purpose. The subsref version is hard to maintain. And requires two helper function calls (to num2cell and to substruct)

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

추가 답변(0개)




Community Treasure Hunt

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

Start Hunting!

Translated by