The code(given) only store last points of intersection.How to store all points of intersection?
1 次查看(过去 30 天)
显示 更早的评论
% code
%%%%Determine point of intersection between perpendicular lines(from curve
%%%%2) and curve 1%%%%%%%%%
clear all; clc;
Y= [318 318 317 317 317 317 317 318 318 319 320 320 320 321 321 321 320 320 320 320 318 318 318 318 318 318];
X = [322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347];
Y1 = [338 338 338 338 338 338 339 339 339 339 339 339 339 339 339 339 339 339 340 340 340 340 340 340 340 340];
for i=1:length(X)
X0 = X(i); %%%%columnwise%%%%%%
n = 100; % 100 or some higher number for nice curves
f = 0.1; % this defines the length of the tangent
x = linspace(min(X),max(X),n);
y2 = interp1(X,Y1,x,'spline');%%%%for smooth curve 1
y = interp1(X,Y,x,'spline'); %%%%for smooth curve 2
xt = X0+(f*(max(X)-min(X))*[-1 0 1]);%%%x coordinates of tangent
m = diff(y)./diff(x);%%%%calculate the slope of the tangent
m = [m, m(end)]; % so just add one at the end
k = min([find(x >= X0,1,'first'), length(x)]);
yt = y(k) + m(k)*(0.1*(max(X)-min(X))*[-1 0 1]);%%%%y coordinates of tangent
%%%%%%%plot normal to the tangent%%%%%5
A=[xt(:,1),yt(:,1)];
B=[xt(:,3),yt(:,3)];
null(A-B);
xa = [A(:,1), B(:,1)];
ya = [A(:,2),B(:,2)];
gg=1.5*(max(Y1)-min(Y));% this defines the length of the normal
normal = [mean(xa),mean(ya)] + gg*null(A-B)';
line([mean(xa),normal(1)],[mean(ya),normal(2)],'color','r','LineWidth',2);
%%%Determine point of intersection of perpendicular lines (from curve2) and curve 1
v1=[mean(xa),normal(1,1)];v2=[mean(ya),normal(1,2)];%%%Points that represents perpedicular lines
[x0,y0,iout,jout] = intersections(v1,v2,x,y2);
plot(X,Y,'ro', x,y,'b',xt,yt,'--g',x,y2,'b'); hold on;
plot(x0,y0,'bo')
end
%%%%%%%%Main aim is to determine max & min distance between two curves.
采纳的回答
Geoff Hayes
2014-6-18
If there is a (for or while) loop that is calculating a particular value at each iteration and you want to save that value, then you will need to store the result in a matrix or cell array. I'm going to suggest the latter in this case to handle the fact that the returned vectors from intersections may be of different size (or empty) at each iteration.
The first thing you need to do is define your cell array which you will do outside of the for loop. Since you know the number of iterations, then you can set the size of the array
n = length(X);
intersectionPoints = cell(n,1);
Now start the work
for i=1:n
X0 = X(i); %%%%columnwise%%%%%%
% etc.
Once you have calculated the intersection points, save them to the cell array
[x0,y0,iout,jout] = intersections(v1,v2,x,y2);
% save x0 and y0
intersectionPoints{i} = [x0 y0]; % I'm assuming that x0 and y0 are column vectors
And that is it. Once the loop ends, the intersectionPoints cell array has all intersection points (if they exist) for each iteration of the loop.
Try the above and see what happens!
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!