How to determine outside perimeter of an array?
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Dyuman Joshi
2022-8-3
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 个评论
更多回答(1 个)
Image Analyst
2022-8-3
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!