How to obtain vertices of all smaller squares in a large square?

6 ビュー (過去 30 日間)
Jonathan Mayers
Jonathan Mayers 2016 年 9 月 27 日
回答済み: Jonathan Mayers 2016 年 9 月 27 日
Hi all,
I am going to generate a square map of side length rs and then divide it up into smaller squares of side length dc using meshgrid. Refer to the image and code below.
coord = 0:dc:rs;
[X,Y] = meshgrid(coord);
How would I obtain the vertices of all smaller squares? Some sort of combination of X and Y must have them but I would like that a new matrix XV is an Nx5 matrix with the x-coordinates of the squares and a matrix YV is an Nx5 matrix with the y-coordinates of the squares. Each row of each matrix would correspond to a different square where N is the number of squares. Also, columns = 5 to duplicate the first column so the vertices are closed and can be used for the inpolygon function.

採用された回答

Jonathan Mayers
Jonathan Mayers 2016 年 9 月 27 日
I have achieved what I wanted with the following code.
function [ XV, YV ] = gen_clusters( rs,dc,X )
%GEN_CLUSTERS Generates the vertices of every cluster in the network.
%
%Input: rs - Side length of network
% dc - Side length of cluster
% X - Matrix X from meshgrid
%
%Output: XV - x-coordinates of clusters
% YV - y-coordinates of clusters
%
%XV and YV are nx5 matrices where n is the number of clusters. 5 columns
%are needed because the 5th column closes the cluster.
% Calculate no. of clusters
n = rs/dc;
% Preallocate
XV = zeros(n,4);
% Calculate XV
for i = 1:n
XV(i,:) = [repmat(X(1,i),1,2) repmat(X(1,i+1),1,2)];
end
% Calculate YV based on XV
YV = repmat(XV,n,1);
YV = circshift(YV,[0 -1]);
YV(:,5) = YV(:,1);
% Duplicate rows of XV n times
XV = reshape(repmat(XV(:)',n,1),[],4);
XV(:,5) = XV(:,1);
end

その他の回答 (1 件)

KSSV
KSSV 2016 年 9 月 27 日
  1 件のコメント
Jonathan Mayers
Jonathan Mayers 2016 年 9 月 27 日
Thank you but the coordinates were not in the form I could work with.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by