Azzera filtri
Azzera filtri

apply arrayfun for a couple of values

5 visualizzazioni (ultimi 30 giorni)
emar
emar il 3 Lug 2017
Commentato: emar il 3 Lug 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 Commenti
Stephen23
Stephen23 il 3 Lug 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 il 3 Lug 2017
Yes, you are right! I edited the mistake

Accedi per commentare.

Risposta accettata

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

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by