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

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Duncan Po
Duncan Po am 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 Kommentare
Duncan Po
Duncan Po am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by