Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Convert the variables x1, x2,... in an anonymous function to x(1,:) ,x(2,:).....

Asked by chen

chen (view profile)

on 28 Mar 2013

For example: f = @(x1, x2, ... , x15) x1 .* x2 .* ... .* x15

I want get: f = @(x) x(1,:) .* x(2,:) .* ... .*x(15,:)

0 Comments

chen

chen (view profile)

Products

2 Answers

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 28 Mar 2013
Edited by Andrei Bobrov

Andrei Bobrov (view profile)

on 28 Mar 2013
x = randi([-5 5],2,10); % this is your data
xc = num2cell(xc,2);
out = f(xc{:});

2 Comments

Matt J

Matt J (view profile)

on 28 Mar 2013

I think this should be

 xc = num2cell(xc,2);
Andrei Bobrov

Andrei Bobrov (view profile)

on 28 Mar 2013

Thank you Matt! Corrected.

Andrei Bobrov

Andrei Bobrov (view profile)

Answer by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 28 Mar 2013
f=@(x) prod(x)

4 Comments

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 28 Mar 2013

If your function is different then change it. t's not clear for me what you want

chen

chen (view profile)

on 28 Mar 2013

Thank you!

For example, I mean how to convert

f = @(x1,x2)sin(x1) .* exp(x2)

to

f = @(x)sin(x(1,:)) .* exp(x(2,:))

automatically in the program.

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 28 Mar 2013

You can't do conversion

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

Contact us