Identify location of the 2D grid data

46 次查看(过去 30 天)
Hi
I have this data and have transformed it into 2D [X, Y].
rectList = [...
1 1 1 1
1 2 1 1
1 3 1 1
1 5 1 1
2 1 1 1
2 2 1 1
2 5 1 1
3 2 1 1
3 3 1 1
4 5 1 1];
xRange = [ min( rectList(:,1)) max( rectList(:,1))];
yRange = [ min( rectList(:,2)) max( rectList(:,2))];
allX = xRange(1):deltaX:xRange(2);
allY = yRange(1):deltaY:yRange(2);
[X, Y] = meshgrid( allX, allY);
Is there to identify logical index of X,Y that belongs to original point, rectList. so it looks like
locOri =
[1 1 0 0
1 1 1 0
1 0 1 0
0 0 0 0
1 1 0 1]
Thanks much

采纳的回答

Bruno Luong
Bruno Luong 2024-11-9,12:53
rectList = [...
1 1 1 1
1 2 1 1
1 3 1 1
1 5 1 1
2 1 1 1
2 2 1 1
2 5 1 1
3 2 1 1
3 3 1 1
4 5 1 1];
deltaX = 1;
deltaY = 1;
xRange = [ min( rectList(:,1)) max( rectList(:,1))];
yRange = [ min( rectList(:,2)) max( rectList(:,2))];
allX = xRange(1):deltaX:xRange(2);
allY = yRange(1):deltaY:yRange(2);
[X, Y] = meshgrid( allX, allY);
locOri = reshape(ismember([X(:),Y(:)],rectList(:,1:2),'rows'), size(X))
locOri = 5x4 logical array
1 1 0 0 1 1 1 0 1 0 1 0 0 0 0 0 1 1 0 1

更多回答(1 个)

Shashi Kiran
Shashi Kiran 2024-11-9,12:43
To identify the logical index of points in the X,Y grid that correspond to the original points in rectList, you can follow these steps:
  • locOri is created as a logical matrix of the same size as X and Y, initially set to false.
% Initialize locOri as a logical array with zeros
locOri = false(size(X));
  • For each point in rectList, find where X and Y match the current point’s coordinates in rectList. Then update locOri to true at those indices.
% Loop through each point in rectList and set corresponding locOri entries to true
for i = 1:size(rectList, 1)
% Find the indices in X and Y that match the current rectList point
xMatch = X == rectList(i,1);
yMatch = Y == rectList(i,2);
% Set locOri to true at positions matching both x and y
locOri = locOri | (xMatch & yMatch);
end
disp(locOri)
1 1 0 0 1 1 1 0 1 0 1 0 0 0 0 0 1 1 0 1
This output has 1s at positions that correspond to the points in rectList and 0s elsewhere, as expected.
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by