# Inconvenience working with matlabFunction

2 views (last 30 days)

Show older comments

Mohammad Shojaei Arani
on 8 Sep 2022

Commented: Mohammad Shojaei Arani
on 21 Sep 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

### Accepted Answer

Star Strider
on 8 Sep 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 Comments

### More Answers (2)

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])

##### 0 Comments

Walter Roberson
on 8 Sep 2022

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!