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

조회 수: 5 (최근 30일)
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

채택된 답변

Duncan Po
Duncan Po 2021년 6월 4일
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
Duncan Po
Duncan Po 2021년 6월 4일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by