Cut around the center of a matrix
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (3)
Image Analyst
am 29 Apr. 2015
Try this:
margin = 1; % Whatever you want.
B = A(margin+1:margin+1, end-margin:end-margin);
2 Kommentare
Image Analyst
am 1 Apr. 2021
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
Michael Haderlein
am 29 Apr. 2015
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 Kommentare
Image Analyst
am 29 Apr. 2015
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.
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!