Problem by creating Sub Matrices
조회 수: 2 (최근 30일)
이전 댓글 표시
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/166882/image.jpeg)
I have 5x5 matrix.I want to create 6 submatrices of order 3x3.Uploaded the pic please any one can do it.Or any other methods to do this operation.Thanks
댓글 수: 0
채택된 답변
Stephen23
2017년 8월 26일
편집: Stephen23
2017년 8월 26일
The more that you set up before the loop, the neater and clearer the code will be:
mat = reshape(1:25,5,5).'; % input matrix, any size
%
blkR = 3; % output block rows
blkC = 3; % output block columns
stpR = 2; % step size rows
stpC = 1; % step size columns
%
inSz = size(mat);
vecR = blkR:stpR:inSz(1); % blocks' end rows
vecC = blkC:stpC:inSz(2); % blocks' end columns
%
numR = numel(vecR); % count blocks along rows
numC = numel(vecC); % count blocks along columns
out = cell(numR,numC);
for kR = 1:numR
for kC = 1:numC
idxR = vecR(kR)+(1:blkR)-blkR; % row indices
idxC = vecC(kC)+(1:blkC)-blkC; % column indices
out{kR,kC} = mat(idxR,idxC); % use indices
end
end
and checking the output:
>> out{:}
ans =
1 2 3
6 7 8
11 12 13
ans =
11 12 13
16 17 18
21 22 23
ans =
2 3 4
7 8 9
12 13 14
ans =
12 13 14
17 18 19
22 23 24
ans =
3 4 5
8 9 10
13 14 15
ans =
13 14 15
18 19 20
23 24 25
>>
추가 답변 (1개)
MSP
2017년 8월 25일
clear all
a=magic(5)
shiftr=2
shiftc=1
k=1
rowsize=3
colsize=3
for i=1:shiftr:size(a,1)
if i+rowsize-1>size(a,1) %%check whether b matrix indices is within a If not then break-i loop
break
end
for j=1:shiftc:size(a,2)
if (j+colsize-1)>size(a,2)
break
else
b(:,:,k)=a(i:i+rowsize-1,j:j+colsize-1)
k=k+1;
end
end
j=1;
end
This is the most basic format of doing it.Have a look and ask if u don't understand.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!