How do I use X&Y Arrays in a formula to get and Array answer?

1 view (last 30 days)
I have a formula L with variables X and Y. X and Y are arrays with eleven numbers in each. I want an array of 121 numbers (11*11) for my answer. But I am only getting eleven answers. (X1,Y1)(X2,Y2)(X3,Y3)......etc. What I want is: (X1,Y1)(X1,Y2)(X1,Y3)......
(X2,Y1)(X2,Y2)(X2,Y3).....etc
Arrays:
x=[15:1:25];
y=[12:1:22];
Formula:
L= sqrt(((x-15).^2)+((y-25).^2))+sqrt(((x-10).^2)+((y-5).^2))+sqrt(((x-40).^2)+((y-10).^2))
Answer:
46.6822 46.2284 45.9273 45.8076 45.9019 46.2441 46.8645 47.7832 49.0065 50.5256 52.3212
How do I set up the arrays X and Y to give me an array answer??
Please help

Accepted Answer

Rik
Rik on 20 Jul 2019
Use ndgrid to generate the full array of combinations. You could also do it with implicit expansion, but that makes it harder to understand.
  3 Comments
Rik
Rik on 20 Jul 2019
That is strange. Both variables should be 11x11 arrays. I haven't noticed any matrix multiplication that would reduce that.
Have you tried splitting the calculations into small steps so you can see where the calculation goes wrong? I'm on mobile so I can't easily test it for you.
Star Strider
Star Strider on 20 Jul 2019
I am also getting an (11x11) matrix with this code:
[x,y] = ndgrid(15:1:25, 12:1:22);
L = sqrt(((x-15).^2)+((y-25).^2))+sqrt(((x-10).^2)+((y-5).^2))+sqrt(((x-40).^2)+((y-10).^2));
figure
surf(x, y, L)
grid on

Sign in to comment.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!