creating table of finely spaced xy points that represent a box corner

1 view (last 30 days)
I'd like operate on an XY table of values that represents one quadrant of a box, but rather than just defining the vertical and horizontal extremes I need a certain resolution of points along the lines of this box because after my math operations it will no longer look like a box. An example is below, but in reality I'll probably have more than 100 points so I can't manually type each one in.
I was thinking of something like this to draw the upper horizontal line:
%upper line
xmax = 5
ymax = 5
ux = [0:1:xmax]
uy = [ymax]
table (ux,uy)
But it doesn't create an equal number of Y values as X. Once I solve that problem my plan was to do the same for the veritical line, and then combine them.
Or is there an easier way where I can define a 2xN array?
An example of the ultimate table is below (but again, I need it to be adjustable and have far finer resolution):
x y
0 5
1 5
2 5
3 5
4 5
5 5
5 5
5 4
5 3
5 2
5 1
5 0

Accepted Answer

Duncan Po
Duncan Po on 4 Jun 2021
meshgrid may be the function you are looking for. For example,
>> [x,y] = meshgrid(1:5,1:5)
x =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
y =
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
The horizontal line will then just be the last row.
>> t = table(x(end,:)',y(end,:)', 'VariableNames', {'x','y'})
t =
5×2 table
x y
_ _
1 5
2 5
3 5
4 5
5 5
  3 Comments
Duncan Po
Duncan Po on 4 Jun 2021
It can work for any real number if you give it a non-integer increment:
>> [x,y] = meshgrid(1:0.1:5,1:0.1:5); % 0.1 increment
>> t = table(x(end,:)',y(end,:)', 'VariableNames', {'x','y'});
>> head(t)
ans =
8×2 table
x y
___ _
1 5
1.1 5
1.2 5
1.3 5
1.4 5
1.5 5
1.6 5
1.7 5

Sign in to comment.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!