How to determine outside perimeter of an array?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sonicflash
el 3 de Ag. de 2022
Respondida: Image Analyst
el 3 de Ag. de 2022
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 comentarios
Respuesta aceptada
Dyuman Joshi
el 3 de Ag. de 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 comentarios
Más respuestas (1)
Image Analyst
el 3 de Ag. de 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 comentarios
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!