How do I use X&Y Arrays in a formula to get and Array answer?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Rik
2019년 7월 20일
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
Rik
2019년 7월 20일
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
2019년 7월 20일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!