Cut around the center of a matrix

조회 수: 54(최근 30일)
I.G
I.G 2015년 4월 29일
댓글: Image Analyst 2021년 4월 1일
Hi everyone,
I have a quick question which may be simple to answer. I have a matrix,
A = [ 5 5 5 5 5 ; 5 4 4 4 5 ; 5 4 4 4 5 ; 5 4 4 4 5 ; 5 5 5 5 5 ]
'A' is always an odd number of lines and columns, and may be 100s of them. I want to cut at my own convenience a portion around the center, like
B = [ 4 4 4 ; 4 4 4 ; 4 4 4 ]
How can I do it?
Thanks

답변(3개)

Image Analyst
Image Analyst 2015년 4월 29일
Try this:
margin = 1; % Whatever you want.
B = A(margin+1:margin+1, end-margin:end-margin);
  댓글 수: 2
Image Analyst
Image Analyst 2021년 4월 1일
Thanks. Fix:
A = [ 5 5 5 5 5 ; 5 4 4 4 5 ; 5 4 4 4 5 ; 5 4 4 4 5 ; 5 5 5 5 5 ]
margin = 1; % Whatever you want.
B = A(margin + 1 : (end - margin), margin + 1 : (end - margin))
A =
5 5 5 5 5
5 4 4 4 5
5 4 4 4 5
5 4 4 4 5
5 5 5 5 5
B =
4 4 4
4 4 4
4 4 4

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


Thorsten
Thorsten 2015년 4월 29일
편집: Thorsten 2015년 4월 29일
szcut = 3;
i1 = (size(A, 1) - szcut)/2;
ind1 = i1+1:i1+szcut;
B = A(ind1, ind2);
If A is not square or if you want to cut out non-square parts, define ind2 analogously.

Michael Haderlein
Michael Haderlein 2015년 4월 29일
If c indicates the range you want to remove and would be 3 in your example, just use
B=A((end-c)/2+1:(end+c)/2,(end-c)/2+1:(end+c)/2);
  댓글 수: 4
I.G
I.G 2015년 4월 29일
I made it like this:
cx=ceil(size(A,1)/2); % cx is the center line cy=ceil(size(A,2)/2); % cy is the center row x= ... % the steps in x axis from center x y= ... % the steps in y axis from center y B=A((cx-y):(cx+y),(cy-x):(cy+x));
Thanks for the help!

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

범주

Find more on Creating and Concatenating Matrices 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!

Translated by