Inconveniences working with matlabFunction

Hello,
I think that I am repeating the same sort of question I bothered you yesterday but I did not expect to run into peoblem again. Consider the following:
>> syms x y
>> g=[x*y y^2;x^3 1-x^2];
>> G=matlabFunction(g)
G =
function_handle with value:
@(x,y)reshape([x.*y,x.^3,y.^2,-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 G([1 1],[1 1]) then matlab throws an erros message. How can I tell matlab PLEASE make the following matlab function for me:
G=(x,y)[x.*y,x.^3,y.^2,-x.^2+1.0];
In my codes G (different G, of course) is the Hessian matrix and I need to use it for my optimization problem in order to get faster and more accurate solutions.
Thanks for your kind help, in advance!
Babak

 採用された回答

Star Strider
Star Strider 2022 年 9 月 8 日

0 投票

The reshape argument simply takes the vector of argumentsin the square brackets [] and creates a matrix from them, as required. One option is to use arrayfun. However, it wil then interpret its inputs as —
GL([x(1) x(2)],[y(1) y(2)])
So changing the arguments so you and the function agree on how they should be claculated will be necessary, regardless of the funciton version you end up using —
G = @(x,y)reshape([x.*y,x.^3,y.^2,-x.^2+1.0],[2,2]); % 'matlabFunction' Result
GL = @(xv,yv) arrayfun(G, xv,yv, 'Unif',0);
V = GL([1 2],[3 4])
V = 1×2 cell array
{2×2 double} {2×2 double}
V{1}
ans = 2×2
3 9 1 0
V{2}
ans = 2×2
8 16 8 -3
G2 = @(x,y)[x.*y y.^2; x.^3 1-x.^2]; % Your Original Function (Vectorised)
V = G2([1 2],[3 4])
V = 2×4
3 8 9 16 1 8 0 -3
G2L = @(xv,yv) arrayfun(G2, xv,yv, 'Unif',0);
V2 = G2L([1 2],[3 4])
V2 = 1×2 cell array
{2×2 double} {2×2 double}
V2{1}
ans = 2×2
3 9 1 0
V2{2}
ans = 2×2
8 16 8 -3
Both end up producing the same result.
.

6 件のコメント

Torsten
Torsten 2022 年 9 月 8 日
@Mohammad Shojaei Arani comment moved here:
Thanks Star!
This solves my problem.
Star Strider
Star Strider 2022 年 9 月 8 日
@Mohammad Shojaei Arani — As always, my pleasure!
@Torsten — Thank you!
Mohammad Shojaei Arani
Mohammad Shojaei Arani 2022 年 9 月 8 日
Hello,
Thanks Star for your kind response. But, I ran into a similar problem again (you correctly answered my previous question). 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
Star Strider
Star Strider 2022 年 9 月 8 日
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))
Hmtx =
Hmtx(:,:,1) = 16 24 31 -6 Hmtx(:,:,2) = 25 35 68 -13
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
.
Mohammad Shojaei Arani
Mohammad Shojaei Arani 2022 年 9 月 9 日
Thanks Star,
Thanks for your kind help!
Babak
Star Strider
Star Strider 2022 年 9 月 9 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by