Inconvenience working with matlabFunction
1 回表示 (過去 30 日間)
古いコメントを表示
Mohammad Shojaei Arani
2022 年 9 月 8 日
コメント済み: Mohammad Shojaei Arani
2022 年 9 月 21 日
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 件のコメント
Steven Lord
2022 年 9 月 8 日
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.
採用された回答
Star Strider
2022 年 9 月 8 日
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 件のコメント
その他の回答 (2 件)
David Hill
2022 年 9 月 8 日
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 件のコメント
Walter Roberson
2022 年 9 月 8 日
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 件のコメント
Walter Roberson
2022 年 9 月 19 日
Huh. I would have guessed that it would be the 1 that should be duplicated, not the 30.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!