How to sort an array into segments
1 view (last 30 days)
Show older comments
I have two arrays, one lists X coordinates of a curve and the other lists the corresponding Y coordinates. I am trying to run the arrays through a script that sorts them into distinct segments with a constant sign of curvature for each segment, with each column being a distinct segment. Constant amounts of points in the each column does not need to be preserved since MatLab fills zeroes in for blank spaces in shorter columns. My code looks something like
m=1;
b=2;
Zx(1,1)=X(1);
Zy(1,1)=Y(1);
[n ~]=size(X);
for a=2:n-1
Zx(b,m)=X(a);
Zy(b,m)=Y(a);
tsx=X(a)-X(a-1);
tsy=Y(a)-Y(a-1);
if tsx>0&&tsy>0
tsx=X(a+1)-X(a);
tsy=Y(a+1)-Y(a);
if tsx>0&&tsy>0
b=b+1;
else
m=m+1;
b=2;
Zx(1,m)=X(a);
Zy(1,m)=Y(a);
end
elseif tsx>0&&tsy<0
so on and so forth, it carries on for each combination of both of them being less than, greater than or equal to zero.
My problem with the code is that after it is done generating the segments (the one I am testing right now creates 484 of them) a few of them come up to just be a segment of two points, but it is just the same point repeated. It still has the end point of the m-1th segment start the m+1th segment, where the mth segment is the one that is the repeated point.
My initial thought was that the initial arrays listing points had repeating points, but I ran a script through that eliminates consecutive repeated points and the problem persists.
0 Comments
Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!