apply arrayfun for a couple of values

9 visualizaciones (últimos 30 días)
emar
emar el 3 de Jul. de 2017
Comentada: emar el 3 de Jul. de 2017
Hello,
I wrote a syntax that calculate values of a function in different values. For example
x1=[1 2 10 11];
x2=[10 11 12 14];
C= arrayfun (@(t1,t2) myfunction(A,B,t1,t2),x1,x2,'UniformOutput',0);
% A and B are matrixs
In this example the function will do an operation on A(x1,x2) and B(x1,x2) . The problem is that arrayfun will work on each couple (x1(1),x2(1)), x1(2),x2(2)),etc. But I want it to work on all the values of x1 and x2 (16 couples of values so that it can be applied also to for example (x1(1),x2(3))).
Is there any way to do that without a loop?
Thank you in advance
  2 comentarios
Stephen23
Stephen23 el 3 de Jul. de 2017
x1=[1 2 10 11];
x2=[10 11 12 14];
C= arrayfun (@(x1,x2) myfunction(A,B,t1,t2),x1,x2,'UniformOutput',0);
You define an anonymous function in two variables x1 and x2 but you never use these variables inside the function. The four variables used inside the functions are not defined anywhere. Even more confusingly the two arrays have the same names as the anonymous function's variables. Did you really mean this?:
C = arrayfun (@(t1,t2) myfunction(A,B,t1,t2),x1,x2,'UniformOutput',0);
emar
emar el 3 de Jul. de 2017
Yes, you are right! I edited the mistake

Iniciar sesión para comentar.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 3 de Jul. de 2017
x1=[1 2 10 11];
x2=[10 11 12 14];
[x,y] = ndgrid(x1,x2)
C = arrayfun (@(t1,t2) myfunction(A,B,t1,t2),x,y,'UniformOutput',0);

Más respuestas (0)

Categorías

Más información sobre MPC Design en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by