Make matrix of N number of (x,y) points

10 views (last 30 days)
Ni2
Ni2 on 27 Oct 2019
Edited: Ni2 on 28 Oct 2019
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
  6 Comments
Adam Danz
Adam Danz on 27 Oct 2019
"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.
Ni2
Ni2 on 27 Oct 2019
Edited: Ni2 on 27 Oct 2019
L1, L2, W1, W2 and D can be fractions and the missing rectangle is always on the top right corner.
The example above has
L1 = 6.75, L2 =3.35, W1=8, W2=4.5 and D=1
Suppose i want to choose 5 random points along perimeter.
I can expect (0, 0), (4, 0), (5, 3.5), (2,8) and (3.4, 7) as one of my output
Suppose i want to choose 5 random points not along perimeter.
I can expect (1, 1), (6, 2), (2, 5), (3, 3) and (3, 7) as one of my output
my motive is to plot some dots at that spots.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 27 Oct 2019
Edited: Adam Danz on 27 Oct 2019
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()
*"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 Comments
Adam Danz
Adam Danz on 27 Oct 2019
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.
Ni2
Ni2 on 28 Oct 2019
Edited: Ni2 on 28 Oct 2019
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.
But I don't have any coordinates matrix, I have only six vertex points. Please check my codes above.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!