To split a matrix into equal parts.
11 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!