To split a matrix into equal parts.

60 views (last 30 days)
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!

Accepted Answer

Friedrich
Friedrich on 4 Jan 2012
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)'

More Answers (1)

David Young
David Young on 4 Jan 2012
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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!