Convhull function returns indices that correspond to NaN values

1 view (last 30 days)
I'm trying to calculate which points contribute to the convex hull of a movement trajectory. Coordinates of the movement trajectory are organised in a matrix (10000x360). Each colom is a different trajectory, each row is a data point. The trajectories all have a different length, so when there is no data anymore for that colom, the rest is notated as NaN's.
For some trajectories this works really well, but also for some I get an indices which corresponds to a NaN value. So the convex hull uses a point that does not exist. How can this happen and how to prevent?
for i = 1:360 % 360 trajectories
m = Indices(i); % Indices gives the last value which is not a NaN of each trajectory (i).
P = [x(1:m,i),y(1:m,i)]; %combining x and y, of not NaN values.
indicesCHull =convhull(P);
XCHull =x(indicesCHull);
YCHull =y(indicesCHull);
% more following here, but not relevant I guess
end
  1 Comment
Image Analyst
Image Analyst on 1 Sep 2022
Attach the (10000x360) matrix in a .mat file.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Sign in to comment.

Answers (2)

Moksh
Moksh on 12 Sep 2023
Edited: Moksh on 12 Sep 2023
Hi Sanne,
I understand that you would like to create a convex hull for each trajectory represented by columns in the “x” and “y” matrices. Since matrices in MATLAB require fixed column sizes, you have filled the empty entries with NaN values.
Instead of maintaining a separate index array to track non-NaN values, you can utilize a “cell array” in MATLAB. Cell arrays allow for storing variable-sized vectors. By using a cell array, you can apply the "convhull" function to each trajectory without generating any NaN values.
Here is an example code for how you can convert this 2D matrix to a cell array:
% Initialize a cell array for maintaining clean x and y trajectory values
XcellArray = cell(1, size(x, 2));
YcellArray = cell(1, size(y, 2));
% Iterate over each column
for col = 1:size(x, 2)
% Extract the x, y column data
XcolumnData = x(:, col);
YcolumnData = y(:, col);
% Remove NaN values from x and y columns
XcolumnData = XcolumnData(~isnan(XcolumnData));
YcolumnData = YcolumnData(~isnan(YcolumnData));
% Store the column data as a vector in the cell array
XcellArray{col} = XcolumnData;
YcellArray{col} = YcolumnData;
end
% Now proceed with the convhull function on clean trajectory values
for col = 1:size(x, 2)
x_traj = XcellArray{col}; % Extract clean x and y trajectory values
y_traj = YcellArray{col};
P = [x_traj, y_traj]; % Combining x and y, of not NaN values.
indicesCHull =convhull(P);
XCHull =x_traj(indicesCHull); % Populate the convhull coordinates on the clean trajectory values
YCHull =y_traj(indicesCHull);
% Following logic
end
In the above code, I have assumed that whenever there is a NaN value in the x column, there is also a corresponding NaN value in the y column, and vice versa. If this assumption does not hold, and there are instances where NaN values exist in one column but not the other, you will need to take the intersection of the indexes in both columns that point to the non-NaN values.
Please refer to the below documentation for more information about “cell-arrays” and isnan” function:
Hope this helps!
Best Regards,
Moksh Aggarwal

Bruno Luong
Bruno Luong on 12 Sep 2023
You must index the column where the convhull is inquired
XCHull =x(indicesCHull,i);
YCHull =y(indicesCHull,i);

Categories

Find more on Bounding Regions in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!