Inconvenience working with matlabFunction
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Mohammad Shojaei Arani
il 8 Set 2022
Commentato: Mohammad Shojaei Arani
il 21 Set 2022
Hello,
One of you did kindly respond my question (my question was very similar to this one) but I again ran into problem and need to bother you again. Sorry for this. Consider the following
>> syms x y p1 p2
h=[x*y+p1 y^2-1;x^3+p2^2 1-x^2+p1*p2];
H=matlabFunction(h)
H =
function_handle with value:
@(p1,p2,x,y)reshape([p1+x.*y,p2.^2+x.^3,y.^2-1.0,p1.*p2-x.^2+1.0],[2,2])
I am not happy with this. I would prefer the 'reshape' to be removed. Why? because this is never convenient in letting me to to do vector operations. For instance, if I use the command H(1,2,[3 4],[5 6]) then matlab throws an erros message. How can I tell matlab PLEASE make the following matlab function for me:
H=@(p1,p2,x,y)[x*y+p1 y^2-1;x^3+p2^2 1-x^2+p1*p2];
I know how to tackle this problem when all inputs p1,p2,x,y have the same size (I can fix it by the command HL = @(p1v,p2v,xv,yv) arrayfun(H, p1v,p2v,xv,yv, 'Unif',0);).
However, here p1 and p2 are scalar parameters while x and y are (row) vectors of the same size (like H(1,2,[3 4],[5 6]), as I mentioned above). The best way to tackle this problem for me is as follows:
I would prefer the output to be a 3D array where the first page is H(1,2,3,5) and the second page is H(1,2,4,6).
Thanks for your kind help, in advance!
Babak
2 Commenti
Steven Lord
il 8 Set 2022
Since this question appears to be a continuation of https://www.mathworks.com/matlabcentral/answers/1800275-inconveniences-working-with-matlabfunction you should probably add comments to that question (which has already received several comments and answers) rather than posting a new question.
Risposta accettata
Star Strider
il 8 Set 2022
Reposting here —
This is an application of passing extra parameters. As such, it now needs a separate function call in order to work correctly.
Try this —
Hmtx = cat(3, Hfcn(1,2,3,5), Hfcn(1,2,4,6))
function M = Hfcn(p1,p2,v1,v2)
H = @(p1,p2,x,y)reshape([p1+x.*y,p2.^2+x.^3,y.^2-1.0,p1.*p2-x.^2+1.0],[2,2]);
C = arrayfun(@(xv,yv)H(p1,p2,xv,yv),v1,v2, 'Unif',0);
M = cell2mat(C);
end
.
0 Commenti
Più risposte (2)
David Hill
il 8 Set 2022
If you don't need to work in symbolics, don't.
h=@(p1,p2,x,y)[x.*y+p1,y.^2-1;x.^3+p2^2, 1-x.^2+p1*p2];
h(1,2,[3 4],[5 6])
0 Commenti
Walter Roberson
il 8 Set 2022
Sorry, but matlabFunction() is not going to change about this, as it involves a fundamental model of what symbolic variables are and how they are manipulated.
8 Commenti
Walter Roberson
il 19 Set 2022
Huh. I would have guessed that it would be the 1 that should be duplicated, not the 30.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!