필터 지우기
필터 지우기

Problem by creating Sub Matrices

조회 수: 1 (최근 30일)
Haseeb Hassan
Haseeb Hassan 2017년 8월 25일
편집: Stephen23 2017년 8월 26일
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

채택된 답변

Stephen23
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
Haseeb Hassan
Haseeb Hassan 2017년 8월 26일
Thanks for your answer, please can you put the comments for more understanding as i am new bie in matlab and programming.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

MSP
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.
  댓글 수: 1
Haseeb Hassan
Haseeb Hassan 2017년 8월 26일
편집: Haseeb Hassan 2017년 8월 26일
Thanks mate, the program working properly, but i am new to this plateform please help to understand me more clearly if you dont mind.I have problem to understand the logic of for loop. Please expalin more with clear information thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by