How to obtain the outermost elements of a 2D matrix ?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everyone,
How can I obtain the outermost elements of a 2D matrix ?
(For example, the elements that are highlighted in the following picture)

0 commentaires
Réponse acceptée
Akira Agata
le 12 Oct 2022
How about the following?
% Sample matrix
M = magic(5);
% Replace non-outermost element as NaN
M(2:end-1, 2:end-1) = nan;
% Extract the outermost elements
idx = isnan(M);
OuterElements = M(~idx)
2 commentaires
Akira Agata
le 12 Oct 2022
OK, then how about the follwing solution?
% Sample matrix
M = magic(5)
% Extract the outermost elements in anti-clockwise manner
OuterElements = M(1:end-1, 1);
for kk = 1:3
M = rot90(M, -1);
OuterElements = [OuterElements; M(1:end-1, 1)];
end
% Show the result
OuterElements
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!