How to determine outside perimeter of an array?
6 views (last 30 days)
Show older comments
For example, a matrix, a:
a = randi(10,5)
How would I obtain the elements of the matrix into an row vector, minus the center 3 x 3?
So in this case:
a_perim = [6 9 2 8 7 8 3 4 10 6 8 5 8 2 4 6]
A previous method I tried was defining the center as new matrix, and using ismember(). However, this did not work when elements in the center 3 x 3 were the same as the perimeter.
0 Comments
Accepted Answer
Dyuman Joshi
on 3 Aug 2022
Note - a_perim vector doesn't correspond to the outer elements of a in the example you mentioned.
Here's how you can achieve that
y=spiral(5) %random matrix
z=ones(5);
z(2:end-1,2:end-1)=0;
y(z==1)'
0 Comments
More Answers (1)
Image Analyst
on 3 Aug 2022
I'm not sure how you're getting
a_perim = [6 9 2 8 7 8 3 4 10 6 8 5 8 2 4 6] ;
from this:
a = [...
3 2 6 6 4
1 7 10 4 7
8 5 1 8 3
7 10 10 3 8
9 10 4 8 1];
Anyway, you can get the perimeter values as [top row, right column, bottom row, left column] like this
topRow = a(1,:);
rightCol = a(2:end-1, end);
bottomRow = a(end, end-1 : -1 : 2);
leftCol = a(end-1 : -1 : 2, 1);
% Get perimeter clockwise from upper left (1,1) element.
a_perim = [topRow, rightCol', bottomRow, leftCol']
If you want a different ordering, let me know what it is.
0 Comments
See Also
Categories
Find more on Matrices and Arrays 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!