To split a matrix into equal parts.
이전 댓글 표시
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!
채택된 답변
추가 답변 (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.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!