MATLAB Answers

I.G
0

Cut around the center of a matrix

Asked by I.G
on 29 Apr 2015
Latest activity Commented on by I.G
on 29 Apr 2015
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

  0 Comments

Sign in to comment.

Tags

3 Answers

回答者: Image Analyst
2015 年 4 月 29 日

Try this:
margin = 1; % Whatever you want.
B = A(margin+1:margin+1, end-margin:end-margin);

  0 Comments

Sign in to comment.


回答者: 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.

  0 Comments

Sign in to comment.


回答者: 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 Comments

Show 1 older comment
Thorsten
2015 年 4 月 29 日
Please see my answer above.
Image Analyst
2015 年 4 月 29 日
In general it's B = A(row1:row2, column1:column2). You can decide on the rows and columns in any way that you want. If you want to get only a particular value (like 4) automatically (even if it's not a rectangle) then let me know. It's possible.
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!

Sign in to comment.