-1

I've been searching the net for a couple of mornings and found nothing, hope you can help.

I have an anonymous function like this

f = @(x,y) [sin(2*pi*x).*cos(2*pi*y), cos(2*pi*x).*sin(2*pi*y)];

that needs to be evaluated on an array of points, something like

x = 0:0.1:1;
y = 0:0.1:1;
w = f(x',y');

Now, in the above example everything works fine, the result w is a 11x2 matrix with in each row the correct value f(x(i), y(i)). The problem comes when I change my function to have constant values:

f = @(x,y) [0, 1];

Now, even with array inputs like before, I only get out a 1x2 array like w = [0,1]; while of course I want to have the same structure as before, i.e. a 11x2 matrix.

I have no idea why Matlab is doing this...

EDIT 1 Sorry, I thought it was pretty clear from what I wrote in the original question, but I see some of you asking, so here is a clarification: what I want is to have again a 11x2 matrix, since I am feeding the function with arrays with 11 elements. This means I expect to have an output exactly like in the first example, just with changed values in it: a matrix with 11 rows and 2 columns, with only values 0 in the first column and only values 1 in the second, since for all x(i) and y(i) the answer should be the vector [0,1]. It means I expect to have:

w = [0 1
     0 1
     0 1
     ...
     0 1]

seems pretty natural to me...

F. Remonato
  • 248
  • 2
  • 12
  • 2
    Why would you expect `@(x,y) [0, 1]` to return enything else but `[0,1]`????? – Shai Oct 27 '13 at 09:44
  • i have updated the question with a clarification – F. Remonato Oct 27 '13 at 10:27
  • I have no idea why this is downvoted. I think that this is a totally reasonable question for someone coming to Matlab form a more mathematical background. – Chris Taylor Oct 27 '13 at 11:07
  • Thanks Chris. Although I have used Matlab for a number of years now, sometimes I still fail to comprehend the way it behaves sometimes. But now I get why this was not working, the point is in how matlab uses vector-input arguments, which is just different from what I imagined. – F. Remonato Oct 27 '13 at 11:59

2 Answers2

1

You are defining a function f = @(x,y) [0, 1]; which has the input parameters x,y and the output [0,1]. What else do you expect to happen?

Update:

This should match your description:

g=@(x,y)[zeros(size(x)),ones(size(y))]
g(x',y')
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • yeah thank you... I thought matlab would return a value the same size of the inputs by default, but now i think i'm starting to see the flaw in my reasoning. Thanks for the help – F. Remonato Oct 27 '13 at 10:52
0

Defining an anonymous function f as

f = @(x,y) [0,1];

naturally returns [0,1] for any inputs x and y regardless of the length of those vectors.

This behavior puzzled me also until I realized that I expected f(a,b) to loop over a and b as if I had written

for inc = 1:length(a)
    f(a(inc), b(inc))
end

However, f(a,b) does not loop over the length of its inputs, so it merely returns [0,1] regardless of the length of a and b.

The desired behavior can be obtained by defining f as

g=@(x,y)[zeros(size(x)),ones(size(y))]

as Daniel stated in his answer.

RobotRaven
  • 130
  • 4