- Define "midpoint". Is this a median? mean? center of mass?
- In your quesiton, I'm assuming C is a scalar value (not a vector but a single data point). So how is that combined with A in D= mid of (A,C)?
How can i write recursive function using midpoint equation 3d coordinate system?
8 次查看(过去 30 天)
显示 更早的评论
A,B are 2 inputs.with this 2 inputs i want to generate n number of midpoints
A = [2 2 2];
B = [13 13 13];
c = mid of (A,B);
D = mid of (A,C);
E = mid(C,B);
how can i write this program in recursive function to calculate n number of midpoints...
and each midpoint distance is>1
1 个评论
Adam Danz
2019-12-4
编辑:Adam Danz
2019-12-6
Your examples of inputs A and B are helpful but please provide an example of expected outputs so we have a complete picture.
----[update after your question-edit]----
By 'midpoint' do you mean you want the (x,y,z) coordinate of the midpoint between the line whose endpoints are A and B?
采纳的回答
Adam Danz
2019-12-4
编辑:Adam Danz
2019-12-5
This solution creates an anonymous function midPointFcn() that receives two inputs A and B which are both (x,y,z) coordinates of a line. The output are the 3 midpoints from your question: C, D, & E.
Then I plot the results for confirmation.
See inline comments for more detail
% INPUTS: (x,y,z) endpoints of a line AB
A = [2 2 2];
B = [13 13 13];
% Create midpoint function
% Inputs A and B are 1x3 (x,y,z) coordinates.
% Output is 3x3 where columns are (x,y,z) coordinates.
% Row 1 is the mid point of AB
% Row 2 is the mid points between A and midpoint(AB)
% Row 3 is the mid points between B and midpoint(AB)
midPointFcn = @(A,B)[mean([A;B]); mean([A;mean([A;B])]); mean([B;mean([A;B])])];
% Compute midpoints C,D,&E
CDE = midPointFcn(A,B);
% Plot results
clf()
plot3([A(1),B(1)],[A(2),B(2)],[A(3),B(3)],'k-o','DisplayName', 'Line AB')
grid on
hold on
plot3(CDE(1,1),CDE(1,2),CDE(1,3),'m*','DisplayName','Midpoint C')
plot3(CDE(2,1),CDE(2,2),CDE(2,3),'b*','DisplayName','Midpoint D')
plot3(CDE(3,1),CDE(3,2),CDE(3,3),'r*','DisplayName','Midpoint E')
legend()
4 个评论
Adam Danz
2019-12-9
A recursive function is just a function that calls itself. Check out tue midPointFcn function in my answer. it just receives two coordinates A and B and determines the midpoint between A and B. If you want that to be recursive, you could create a while-loop that does the following
- It starts with the two end points A and B as the initial inputs and produces C which is the midpoint of AB.
- Then the new inputs are A and C which finds the midpoint D of AC.
- Then the new inputs are A and D etc.....
- After each midpoint calculation, you calculate the distance and when it falls below threshold. you stop.
- But then you have to do the same thing from the other end where the inputs are (B,C) etc...
If you get stuck, show us how far you got and we can help you get unstuck.
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!