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

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
and
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.

 Akzeptierte Antwort

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 Kommentare

Thanks for your explanation. So, do you mean that the single line of my code (i.e. that has the function deal) can't be edited to make the output of vpasolve be double-casted and distributed to the two variables without extra line/process?
syms syma symb
eqns = [...
syma*exp(symb)== 1,...
syma*exp(symb*1500) == 5,...
];
vars = [syma symb ];
[sola, solb] = subsref(cellfun(@double, struct2cell(vpasolve(eqns, vars)), 'uniform', 0), substruct('{}', {':'}))
sola = 0.9989
solb = 0.0011
However I would suggest that using an auxillary function such as
Split = @(V) V{:};
[sola, solb] = Split(cellfun(@double, struct2cell(vpasolve(eqns, vars)), 'uniform', 0))
sola = 0.9989
solb = 0.0011
makes the code a lot shorter and easier to understand. If you are working with artificial conditions that force you to use subsref() yourself because you are not "allowed" to use a helper function, then you are probably doing something wrong.
@Walter Roberson May I know why the following doesn't work to distribute each vector to the corresponding output variable? and how to fix it?
nv = [-0.7071; 0; -0.7071];
[bv1, bv2] = subsref(num2cell(null(nv')), substruct('{}', {':',1:2}))
It is not clear which vectors you are trying to output. If you are trying to break the result of null(nv') up by column then one way is
nv = [-0.7071; 0; -0.7071];
[bv1, bv2] = subsref(num2cell(null(nv'),1), substruct('{}', {':',':'}))
bv1 = 3×1
0 1 0
bv2 = 3×1
-0.7071 0 0.7071
But I do not recommend this.
You are right about the intended output vectors. If you don't mind, why shouldn't I do it this way instead of the traditional way?
bv = null(nv');
bv1 = bv(:,1);
bv2 = bv(:,2);
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)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by