フィルターのクリア

How to determine blocks in mesh grid?

2 ビュー (過去 30 日間)
Xiaohan Du
Xiaohan Du 2016 年 5 月 18 日
コメント済み: Guillaume 2016 年 5 月 19 日
Hello guys,
I have a matrix representing a grid, say
A = [-1 -1; -1 1; 1 -1; 1 1; -1 0; 0 -1; 0 0; 0 1; 1 0];
Every row of A is a Cartesian coordinate. Therefore if you plot a scatter, you'll find that A is a square. My question is: clearly there are four blocks in A: left upper, right upper, left lower, right lower. How can I write a code to generate 4 matrices which each of them represents one block? The result should be something like:
A1 = [-1 -1; 0 -1; -1 0; 0 0],
A2 = [0 -1; 1 -1; 0 0; 1 0],
A3 = [-1 0; 0 0; -1 1; 0 1],
A4 = [0 0; 1 0; 0 1; 1 1].
Thanks!
  2 件のコメント
Image Analyst
Image Analyst 2016 年 5 月 19 日
I think you just did it.
Xiaohan Du
Xiaohan Du 2016 年 5 月 19 日
Hi Image Analyst,
Thanks for the reply!
I know what the answer should be like, and I can do it manually. But with the given matrix, what is the algorithm behind it? What if I have a finer grid? I cannot do it manually forever, it needs to be automatic.

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

採用された回答

Guillaume
Guillaume 2016 年 5 月 19 日
編集済み: Guillaume 2016 年 5 月 19 日
This should work:
A = [-1 -1; -1 1; 1 -1; 1 1; -1 0; 0 -1; 0 0; 0 1; 1 0];
%get the sorted unique x and y coordinates:
xy = cellfun(@unique, num2cell(A, 1), 'UniformOutput', false);
x = xy{1};
y = xy{2};
%create output
Agrid = cell(numel(y) - 1, numel(x) - 1);
%iterate through the grid and fill output
for xidx = 1 : numel(x) - 1
for yidx = 1 : numel(y) - 1
Agrid{yidx, xidx} = [repmat(x(xidx:xidx+1), 2, 1), repelem(y(yidx:yidx+1), 2)];
end
end
  3 件のコメント
Xiaohan Du
Xiaohan Du 2016 年 5 月 19 日
Hi Guillaume,
I just tested, this works for a finer grid as well! Thank you so much!
Guillaume
Guillaume 2016 年 5 月 19 日
This works for grids of any size, square or non-square and uniform or non-uniform.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSources についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by