Convhull function returns indices that correspond to NaN values

조회 수: 1 (최근 30일)
Sanne Maas
Sanne Maas 2022년 9월 1일
편집: Moksh 2023년 9월 12일
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
Image Analyst
Image Analyst 2022년 9월 1일
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:

댓글을 달려면 로그인하십시오.

답변 (2개)

Moksh
Moksh 2023년 9월 12일
편집: Moksh 2023년 9월 12일
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 2023년 9월 12일
You must index the column where the convhull is inquired
XCHull =x(indicesCHull,i);
YCHull =y(indicesCHull,i);

카테고리

Help CenterFile Exchange에서 Bounding Regions에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by