apply arrayfun for a couple of values

5 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2017-7-3
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 2017-7-3
Yes, you are right! I edited the mistake

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2017-7-3
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MPC Design 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by