apply arrayfun for a couple of values

11 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 3 Jul 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 on 3 Jul 2017
Yes, you are right! I edited the mistake

Sign in to comment.

Accepted Answer

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

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!