Inconvenience working with matlabFunction

3 views (last 30 days)
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 =
function_handle with value:
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!
Mohammad Shojaei Arani
Mohammad Shojaei Arani on 8 Sep 2022
Hi Steven,
Sorry for this. I continue with my former question (which I did already).

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 8 Sep 2022
I responded to your original post and Comment.
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))
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);

More Answers (2)

David Hill
David Hill on 8 Sep 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])
ans = 2×4
16 25 24 35 31 68 -6 -13

Walter Roberson
Walter Roberson on 8 Sep 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.
Mohammad Shojaei Arani
Mohammad Shojaei Arani on 21 Sep 2022
No. The correction should be for the elements we have mismatch issue which is 30. This was the point
in my comments: if we get dimensional mismatch becose one component, say component i, does not match the rest then the correction should be performed in a way to replicate the 'non-matching' parts of compontnt i to the rest of dimensions. Well, it is not unique, unlike what I said before but based on the mentioned 'principle' it seems to be a unique cvorection. Anyway, I find this useful and a very natural and useful correction to the mismatch dimensional problems.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!