Make matrix of N number of (x,y) points
조회 수: 4 (최근 30일)
이전 댓글 표시
I want to select N random points of intersection without repetition.
1. any intersection point
2. only along perimeter
The gap between the lined horizontally and vertically are same 'D'
The number of lines may differ from that of the picture, depending on values of L1, L2, W1, W2 and D
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/245177/image.jpeg)
댓글 수: 6
Adam Danz
2019년 10월 27일
"Yes bottom corner is (0,0) for ease.; This grid can be anywhere in space though."
That sounds like "yes, but no". I'll assume the bottom, left corner is always at (0,0). If that changes it would be ease to shift the results accordingly.
What about the answers to the other questions (I added more)? If you could use the question numbers to reference the answer to those question, it would help to keep things organized.
답변 (1개)
Adam Danz
2019년 10월 27일
편집: Adam Danz
2019년 10월 27일
Here's a solution following your definitions.
At the top of the code you can defin the input values W1 L1 W2 L2 D and the coordinates of the bottom, left of the grid.
- C is an n x 2 matrix of [x,y] coordinates for all grid points
- A is an m x 2 matrix of [x,y] coordinates for the perimeter points
- B is a p x 2 matrix of [x,y] coordinates for the inner grid points
At the end a plot is created to visually confirm that C, A and B are correct.
See inline comments.
See more text under the figure.
% Define main rectangle
W1 = 8.8; %width
L1 = 9.8; %Height (or length)
% Define missing rectangle in upper, right corner
W2 = 4.2;
L2 = 6.6;
% Define grid space
D = 1;
% Coordinate of bottom, left of main rectangle
BL = [0,0]; %[x,y] coordinates of Bottom Left
%% Define grid points
% grid locations for main rectangle
% Later we'll remove the grid points that are defined by L2 and W2
L1pts = unique([BL(1) : D : W1, W1]);
W1pts = unique([BL(2) : D : L1, L1]);
% [L1W1x(n), L1W1y(n)] defines the n_th grid location of the main rectangle.
[L1W1x, L1W1y] = meshgrid(L1pts, W1pts);
% Grid endpoints for the missing rectangle
W2left = W1-W2;
L2bottom = L1-L2;
% Remove coordinates from the main rectangle that are
% within the missing rectangle by replacing thier
% values with NaNs
isInMissingRec = (L1W1x > W2left) & (L1W1y > L2bottom);
L1W1x(isInMissingRec) = NaN;
L1W1y(isInMissingRec) = NaN;
% Plot out result to make sure things make sense.
figure()
plot(L1W1x(:), L1W1y(:), 'ko')
axis equal
grid on
set(gca, 'XTick', unique(L1W1x(~isnan(L1W1x))), 'YTick', unique(L1W1y(~isnan(L1W1y))));
%% Output intersections
% all intersections
% C is a n x 2 matrix of [x,y] coordinates for each intersection
C = unique([L1W1x(:), L1W1y(:)],'rows');
C(any(isnan(C),2),:) = [];
% Perimeter coordinates
bottomTopPerim = cell2mat(splitapply(@(xy){[xy(1,1),min(xy(:,2)); xy(1,1),max(xy(:,2))]}, C,findgroups(C(:,1))));
leftRightPerim = cell2mat(splitapply(@(xy){[min(xy(:,1)), xy(1,2); max(xy(:,1)), xy(1,2)]}, C,findgroups(C(:,2))));
% A is a m x 2 matrix of [x,y] coordinates for each parimteter coordinate
A = unique([bottomTopPerim; leftRightPerim],'rows');
% Inner coordinates
% B is a p x 2 matrix of [x,y] coordinates for each non-perimeter coordinate
B = C(~ismember(C, A, 'rows'),:);
% Plot the results confirm it makes sense
figure()
plot(B(:,1),B(:,2),'bo', 'DisplayName', 'Inner')
hold on
plot(A(:,1),A(:,2),'rs', 'DisplayName', 'Perimeter')
plot(C(:,1),C(:,2),'k.', 'DisplayName', 'All')
axis equal
grid on
set(gca, 'XTick', unique(L1W1x(~isnan(L1W1x))), 'YTick', unique(L1W1y(~isnan(L1W1y))));
legend()
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/245182/image.png)
*"Innter" in legend is a type, it was fixed in the code.
Now that you've got A, B and C, it's easy to choose random coordinates.
Here' a line that chooses 5 random coordinates from C
rc = C(randsample(size(C,1), 5),:);
댓글 수: 8
Adam Danz
2019년 10월 27일
But none of that matters if you already have the coordinates of the grid. If you already have the coordinates of the grid you need to tell me what format they are in.
For example, are your coordinates stored in an n-by-2 matrix of [x,y] values?
The most imortant thing to learn here is to describe or supply ALL of the data you're working with in your question. We can't see your monitor and we have no idea what variables you already have. We only have the information you give us.
So you question should be been something like
I have a set of coordinates stored in the variable "grid_values" that is a 128 x 2 matrix of [x,y] values' These values form a grid in the shape of a 6-sided object as shown in the image below. I need to 1) isolate the coordinates that lay along the parimeter and 2) isolate the inter coordinates that are not on the parimeter. Then I need to choose random values from each of those sets of coordinates.
참고 항목
카테고리
Help Center 및 File Exchange에서 Polar Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!