To split a matrix into equal parts.

33 ビュー (過去 30 日間)
Pranjal Pathak
Pranjal Pathak 2012 年 1 月 4 日
Hi, Thanks to your answers, but did not get it properly. I am making it simpler by considering for a 6 by 6 matrix.Suppose I have a 6 by 6 matrix where the elements vary from 1:36 in increment of 1. When I split it into 4 equal matrix the matrices should be:a(say)=[1 2 3; 7 8 9; 13 14 15],b(say)=[4 5 6;10 11 12; 16 17 18],c=[19 20 21;25 26 27; 31 32 33],d=[22 23 24;28 29 30; 34 35 36].Now, when I choose the middle element to construct a new matrix it should be M(say)=[8 11; 26 29]. Now, my question lies simply to generalize it for a 128 by 128 matrix. Thankin you!

採用された回答

Friedrich
Friedrich 2012 年 1 月 4 日
Hi,
still the same answer as before:
a = reshape(1:36,6,6)
b = a(2:3:end,2:3:end)'
So when usingg 128x128 matrix:
a = reshape(1:128*128,128,128);
b = a(16:32:end,16:32:end)'
  1 件のコメント
Pranjal Pathak
Pranjal Pathak 2012 年 1 月 6 日
Thanks a lot sir!!!

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

その他の回答 (1 件)

David Young
David Young 2012 年 1 月 4 日
If your original matrix is m, then provided its size is even on each dimension, you can divide it into four equal pieces like this:
a = m(1:end/2, 1:end/2);
b = m(1:end/2, end/2+1:end);
c = m(end/2+1:end, 1:end/2);
d = m(end/2+1:end, end/2+1:end);
You can check that the pieces are correct by printing their sizes, and also by seeing that they reassemble to give the original, with:
isequal(m, [a b; c d])
To get the middle elements of the pieces, it's handy to define a function for the purpose. You can put it in an m-file, or inline like this:
midpoint = @(x) x(ceil(end/2), ceil(end/2));
If the pieces don't have odd sizes, then there isn't really a midpoint. In such cases this function returns an element as close as possible to the midpoint.
You can now build your matrix of midpoints with:
M = [midpoint(a) midpoint(b); midpoint(c) midpoint(d)]
All of this is general for any size of matrix.
  1 件のコメント
Pranjal Pathak
Pranjal Pathak 2012 年 1 月 6 日
Thanks!

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by